将一个字符串中的所有空格替换为%20的源代码及测试用例
来源:互联网 发布:微信对话软件 编辑:程序博客网 时间:2024/04/28 03:39
#include <iostream>#include <assert.h>void ReplaceBlank(char string[],int capacity)//capacity为数组str的总容量{if (string == NULL || capacity <= 0)return;int length = 0;int blank_count = 0;int i = 0;while (string[i]!='\0'){length++;if (string[i] == ' '){blank_count++;}i++;}int newLength = length + blank_count * 2;if (newLength > capacity)return;int indexOld = length;int indexNew = newLength;while (indexOld >= 0 && indexNew >= indexOld){if (string[indexOld] == ' '){string[indexNew--] = '0';string[indexNew--] = '2';string[indexNew--] = '%';}else{string[indexNew] = string[indexOld];indexNew--;}--indexOld;}}void Test(char* testName, char string[], int length, char expected[]){if (testName != NULL)printf("%s begins: ", testName);ReplaceBlank(string, length);if (expected == NULL && string == NULL)printf("passed.\n");else if (expected == NULL && string != NULL)printf("failed.\n");else if (strcmp(string, expected) == 0)printf("passed.\n");elseprintf("failed.\n");}// 空格在句子中间void Test1(){const int length = 100;char string[length] = "hello world";Test("Test1", string, length, "hello%20world");}// 空格在句子开头void Test2(){const int length = 100;char string[length] = " helloworld";Test("Test2", string, length, "%20helloworld");}// 空格在句子末尾void Test3(){const int length = 100;char string[length] = "helloworld ";Test("Test3", string, length, "helloworld%20");}// 连续有两个空格void Test4(){const int length = 100;char string[length] = "hello world";Test("Test4", string, length, "hello%20%20world");}// 传入NULLvoid Test5(){Test("Test5", NULL, 0, NULL);}// 传入内容为空的字符串void Test6(){const int length = 100;char string[length] = "";Test("Test6", string, length, "");}//传入内容为一个空格的字符串void Test7(){const int length = 100;char string[length] = " ";Test("Test7", string, length, "%20");}// 传入的字符串没有空格void Test8(){const int length = 100;char string[length] = "helloworld";Test("Test8", string, length, "helloworld");}// 传入的字符串全是空格void Test9(){const int length = 100;char string[length] = " ";Test("Test9", string, length, "%20%20%20");}int main(){Test1();Test2();Test3();Test4();Test5();Test6();Test7();Test8();Test9();return 0;}
0 0
- 将一个字符串中的所有空格替换为%20的源代码及测试用例
- 将一个字符串中的空格全部替换为“%20”
- 将一个字符串中的空格替换为“%20”(java)
- 替换空格(将字符串中的空格替换为20%)
- 将字符串中的空格替换为“20%”
- 将字符串中的空格替换为%20
- 将字符串中的空格替换为“%20”
- 将字符串中的空格替换为%20
- 把一个字符串中的所有的空格替换成“%20”
- 字符串所有连续的空格替换为一个空格
- javascript将字符串中的多个空格替换为一个空格的正则实例
- 将一个字符串中的空格替换为指定的内容——4
- 正则表达式将字符串中的所有连续空格替换成一个空格
- 编写一个方法,将字符串中的空格全部替换为“%20”
- 替换空格 将一个字符串中的空格替换成“ ”
- 1.5 编写代码将字符串中的空格替换为‘%20 ’
- 将一个字符串中的空格替换成“ ”
- 实现一个函数,将字符串中的空格替换成"%20"
- POJ 1678 I Love this Game!(博弈DP)
- web前端学习(2)
- 图像梯度方向直方图学习(1)
- 关于OnLobbyServerSceneLoadedForPlayer和OnLobbyServerCreateGamePlayer在LobbyManager中没有执行的解决办法
- Laravel 之Service Providers
- 将一个字符串中的所有空格替换为%20的源代码及测试用例
- SpringMVC+AJAX+MultipartFile上传单个图片
- Android如何通过TextView实现超链接的跳转
- MyEclipse 优化配置(1)
- sql子查询
- 翻译
- ASP中的REPLACE函数如何不区分大小写
- 浅谈数据挖掘(概论)
- Activiti工作流demo