標籤:new 字元 class 題目 color 完整 網路 start ace
在網路編程中,需要將URL參數中含有的特殊字元通過在‘%‘後加上ASCII碼的兩位十六進位的方法,轉換成伺服器端能夠識別的字元,如空格的ASCII碼為32即16進位的0x20,則需要替換為"%20"。
題目:請實現一個函數,把傳入char*字串中的每個空格替換成"%20",例如輸入"We are happy.",則輸出"We%20are%20happy."
時間複雜度為O(n)的解法思路:
首先遍曆字串,得到字串長度originalLength並統計空格數numberOfBlank,可計算出替換後的字串長度newLength = originalLength+2*numberOfBlank;
從字串的尾部開始複製並向前移動,用兩個下標IndexOfOriginal和IndexOfNew分別標記目前所指的原字元位置和替換後的字元位置;
當IndexOfOriginal指向空格時,IndexOfNew下標所指的字元及其往前1、2個位置分別替換為‘0‘、‘2‘、‘%‘,替換完成後IndexOfOriginal向前移動一格,IndexOfNew相應向前移動3格,否則都往前移動一格;
最後在字串尾部即下標為newLength-1的位置添加‘\0‘。
代碼:
void ReplaceBlank(char str[], int maxLength) { if(str == NULL || maxLength <=0) return; //calculate the new length after replacing and number of Blanks int originalLength = 0; int numberOfBlank = 0; int index = 0; for(index = 0; str[index] != ‘\0‘; index++) { if(str[index] == ‘ ‘) ++numberOfBlank; } originalLength = index+1; //if the number of blank is 0, nothing changes if(numberOfBlank==0) return; //if the new length exceeds the max length int newLength = originalLength+2*numberOfBlank; if (newLength > maxLength) { cout << "new Length exceeds the max Length!" << endl; return; } //start at the character before ‘\0‘ int indexOfOriginal = originalLength-2; int indexOfNew = newLength-2; while (indexOfOriginal>=0 && indexOfNew > indexOfOriginal) { if (str[indexOfOriginal] == ‘ ‘) { str[indexOfNew--] = ‘0‘; str[indexOfNew--] = ‘2‘; str[indexOfNew--] = ‘%‘; } else { str[indexOfNew--] = str[indexOfOriginal]; } --indexOfOriginal; } //add a ‘\0‘ at last str[newLength-1] = ‘\0‘;}
完整代碼:
#include <iostream>#include <stdio.h>using namespace std;void ReplaceBlank(char str[], int maxLength) { if(str == NULL || maxLength <=0) return; //calculate the new length after replacing and number of Blanks int originalLength = 0; int numberOfBlank = 0; int index = 0; for(index = 0; str[index] != ‘\0‘; index++) { if(str[index] == ‘ ‘) ++numberOfBlank; } originalLength = index+1; //if the number of blank is 0, nothing changes if(numberOfBlank==0) return; //if the new length exceeds the max length int newLength = originalLength+2*numberOfBlank; if (newLength > maxLength) { cout << "new Length exceeds the max Length!" << endl; return; } //start at the character before ‘\0‘ int indexOfOriginal = originalLength-2; int indexOfNew = newLength-2; while (indexOfOriginal>=0 && indexOfNew > indexOfOriginal) { if (str[indexOfOriginal] == ‘ ‘) { str[indexOfNew--] = ‘0‘; str[indexOfNew--] = ‘2‘; str[indexOfNew--] = ‘%‘; } else { str[indexOfNew--] = str[indexOfOriginal]; } --indexOfOriginal; } //add a ‘\0‘ at last str[newLength-1] = ‘\0‘;}void test(char str[], int maxLength) { cout << "-----------test-------------" << endl; printf("Original: %s\n", str); ReplaceBlank(str, maxLength); printf("Replaced: %s\n", str); cout << "-----------end---------------" << endl; }int main() { /* char str[50] = "hello world and s"; ReplaceBlank(str, 50); printf("%s\n", str); */ char str1[50]="hello world and s"; char str2[50]=""; char str3[20]="hello world! fdsjkd"; char str4[50]=" helloweroddfsd"; char str5[50]=" "; test(str1, 50); test(str2, 50); test (NULL, 50); test(str3, 20); test(str4, 50); test(str5, 50); return 0;}
參考資料:《劍指Offer名企面試官精講典型編程題》
C++字串空格替換題