程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++完成將一個字符串中的字符調換成另外一個字符串的辦法

C++完成將一個字符串中的字符調換成另外一個字符串的辦法

編輯:關於C++

C++完成將一個字符串中的字符調換成另外一個字符串的辦法。本站提示廣大學習愛好者:(C++完成將一個字符串中的字符調換成另外一個字符串的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++完成將一個字符串中的字符調換成另外一個字符串的辦法正文


本文實例講述了C++完成將一個字符串中的字符調換成另外一個字符串的辦法,分享給年夜家供年夜家參考。詳細辦法以下:

標題請求:

原地完成字符串中的每一個空格調換成"%20",例如輸出"We are happy", 輸入"We%20are%20happy"
被調換的字符串固然不只僅是空格,下面只是個例子
這是道很好的標題,也是百度面試中的一道題,標題不難,然則成績得斟酌周全。這裡給出以下完成代碼:

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

int findNumberFirst(const char *str, const char *dest, vector<int>& pvec)
{
 if (str == NULL || dest == NULL)
 return 0;

 int pos = 0;
 int lenStr = strlen(str);
 int lenDest = strlen(dest);

 if (lenStr < lenDest)
 return 0;

 int count = 0;
 while (pos <= lenStr - lenDest)
 {
 if (strncmp(str + pos, dest, strlen(dest)) == 0)
 {
  pvec.push_back(pos);
  pos += lenDest;
  count++;
 }
 else
 {
  pos++;
 }
 }

 return count;
}

int findNumberLast(const char *str, const char *dest, vector<int> &pvec)
{
 if (str == NULL || dest == NULL)
 return 0;

 int strLen = strlen(str);
 int destLen = strlen(dest);

 if (strLen < destLen)
 return 0;

 int pos = 0;
 while (pos <= strLen - destLen)
 {
 if (strncmp(str + pos, dest, strlen(dest)) == 0)
 {
  pos += destLen;
  pvec.push_back(pos - 1);
 }
 else
 {
  pos++;
 }
 
 }

 return pvec.size();
}

void replaceArray(char *str, const char *src, const char *dest)
{
 if (str == NULL || src == NULL || dest == NULL)
 return;

 vector<int> pvec;
 int strLen = strlen(str);
 int srcLen = strlen(src);
 int destLen = strlen(dest);

 if (strLen < srcLen)
 return;

 int posBefore = 0;
 int posAfter = 0;

 if (srcLen < destLen)
 {
 int count = findNumberLast(str, src, pvec);
 if (count <= 0)
  return;
 
 posAfter = strLen + count * (destLen - srcLen) - 1;
 posBefore = strLen - 1;

 while (count > 0 && posBefore >= 0)
 {
  if (pvec[count - 1] == posBefore)
  {
  posAfter -= destLen;
  strncpy(str + posAfter + 1, dest, strlen(dest));
  count--;
  posBefore--;
  }
  else
  {
  str[posAfter--] = str[posBefore--];
  }
 }
 }
 else if (strLen > destLen)
 {
 int count = findNumberFirst(str, src, pvec);
 if (count <= 0)
  return;

 posAfter = 0;
 posBefore = 0;

 int i = 0;
 while (count >= 0 && posBefore < strLen)
 {
  if (count > 0 && pvec[i] == posBefore)
  {
  strncpy(str + posAfter, dest, strlen(dest));
  posAfter += destLen;
  count--;
  posBefore += srcLen;
  i++;
  }
  else
  {
  str[posAfter++] = str[posBefore++];
  }
 }
 str[posAfter] = '\0';
 }
}

void main()
{ 
 char *str = new char[100];
 if (str == NULL)
 return;
 memset(str, '\0', 100);

 const char *src = " ";
 const char *dest = "%20";
//case1: 只要1個空格
 strcpy(str, " ");

 replaceArray(str, src, dest);
 cout << "str: " << str << endl;

 replaceArray(str, dest, src);
 cout << "str: " << str << endl;

//case2: 兩個空格
 strcpy(str, " ");

 replaceArray(str, src, dest);
 cout << "str: " << str << endl;

 replaceArray(str, dest, src);
 cout << "str: " << str << endl;

//case3: 正常情形
 strcpy(str, "we are happy");

 replaceArray(str, src, dest);
 cout << "str: " << str << endl;

 replaceArray(str, dest, src);
 cout << "str: " << str << endl;

//case3: 空格在前
 strcpy(str, " we are happy");

 replaceArray(str, src, dest);
 cout << "str: " << str << endl;

 replaceArray(str, dest, src);
 cout << "str: " << str << endl;

//case4: 空格在後
 strcpy(str, "we are happy ");

 replaceArray(str, src, dest);
 cout << "str: " << str << endl;

 replaceArray(str, dest, src);
 cout << "str: " << str << endl;

//case4: 沒空格
 strcpy(str, "wearehappy");

 replaceArray(str, src, dest);
 cout << "str: " << str << endl;

 replaceArray(str, dest, src);
 cout << "str: " << str << endl;

//case5: 雙方一樣
 strcpy(str, "we are happy");

 replaceArray(str, src, dest);
 cout << "str: " << str << endl;

 src = "%20";
 assert(dest == "%20");
 replaceArray(str, dest, src);
 cout << "str: " << str << endl;
}

剖析上述代碼,很成心思的一個情形是srcLen和destLen或年夜或小的情況,其界限前提的剖斷紛歧樣。好比we are happy為例子,從後往前拷貝時,count=2。

在count=0時,正好將最後面的空格調換完成,we則不消反復拷貝。然則關於早年往後拷貝,當count=0時,最初面的happy將不會被拷貝。

願望本文所述實例對年夜家C++法式算法設計的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved