利用STL:
1 #include"iostream" 2 #include"stdio.h" 3 #include"algorithm" 4 using namespace std; 5 6 string ReplaceBlank(string src) 7 { 8 if(src.find(" ")>src.length()) 9 return src;10 while(src.find(" ")
官方给出的O(n)复杂度的算法:
1 // 面试题5:替换空格 2 // 题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”, 3 // 则输出“We%20are%20happy.”。 4 5 #include6 #include 7 8 /*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/ 9 void ReplaceBlank(char str[], int length) 10 { 11 if(str == nullptr && length <= 0) 12 return; 13 14 /*originalLength 为字符串str的实际长度*/ 15 int originalLength = 0; 16 int numberOfBlank = 0; 17 int i = 0; 18 while(str[i] != '\0') 19 { 20 ++ originalLength; 21 22 if(str[i] == ' ') 23 ++ numberOfBlank; 24 25 ++ i; 26 } 27 28 /*newLength 为把空格替换成'%20'之后的长度*/ 29 int newLength = originalLength + numberOfBlank * 2; 30 if(newLength > length) 31 return; 32 33 int indexOfOriginal = originalLength; 34 int indexOfNew = newLength; 35 while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal) 36 { 37 if(str[indexOfOriginal] == ' ') 38 { 39 str[indexOfNew --] = '0'; 40 str[indexOfNew --] = '2'; 41 str[indexOfNew --] = '%'; 42 } 43 else 44 { 45 str[indexOfNew --] = str[indexOfOriginal]; 46 } 47 48 -- indexOfOriginal; 49 } 50 } 51 52 // ====================测试代码==================== 53 void Test(char* testName, char str[], int length, char expected[]) 54 { 55 if(testName != nullptr) 56 printf("%s begins: ", testName); 57 58 ReplaceBlank(str, length); 59 60 if(expected == nullptr && str == nullptr) 61 printf("passed.\n"); 62 else if(expected == nullptr && str != nullptr) 63 printf("failed.\n"); 64 else if(strcmp(str, expected) == 0) 65 printf("passed.\n"); 66 else 67 printf("failed.\n"); 68 } 69 70 // 空格在句子中间 71 void Test1() 72 { 73 const int length = 100; 74 75 char str[length] = "hello world"; 76 Test("Test1", str, length, "hello%20world"); 77 } 78 79 // 空格在句子开头 80 void Test2() 81 { 82 const int length = 100; 83 84 char str[length] = " helloworld"; 85 Test("Test2", str, length, "%20helloworld"); 86 } 87 88 // 空格在句子末尾 89 void Test3() 90 { 91 const int length = 100; 92 93 char str[length] = "helloworld "; 94 Test("Test3", str, length, "helloworld%20"); 95 } 96 97 // 连续有两个空格 98 void Test4() 99 {100 const int length = 100;101 102 char str[length] = "hello world";103 Test("Test4", str, length, "hello%20%20world");104 }105 106 // 传入nullptr107 void Test5()108 {109 Test("Test5", nullptr, 0, nullptr);110 }111 112 // 传入内容为空的字符串113 void Test6()114 {115 const int length = 100;116 117 char str[length] = "";118 Test("Test6", str, length, "");119 }120 121 //传入内容为一个空格的字符串122 void Test7()123 {124 const int length = 100;125 126 char str[length] = " ";127 Test("Test7", str, length, "%20");128 }129 130 // 传入的字符串没有空格131 void Test8()132 {133 const int length = 100;134 135 char str[length] = "helloworld";136 Test("Test8", str, length, "helloworld");137 }138 139 // 传入的字符串全是空格140 void Test9()141 {142 const int length = 100;143 144 char str[length] = " ";145 Test("Test9", str, length, "%20%20%20");146 }147 148 int main(int argc, char* argv[])149 {150 Test1();151 Test2();152 Test3();153 Test4();154 Test5();155 Test6();156 Test7();157 Test8();158 Test9();159 160 return 0;161 }