【C++】将字符串中的空格替换成字符
来源:互联网 发布:淘宝网防晒衣 编辑:程序博客网 时间:2024/04/27 21:37
替换字符串中的空格为$$$。要求时间复杂度为O(N)
例如:将”talk is cheap show me the code”替换为:
“talk$$$is$$$cheap$$$show$$$me$$$the$$$code”
思路:将原数组的空格替换为特殊符号,那么原数组的大小也改变了,因此我们需要定义新的字符串大小,因为将空格替换为$$$原数组大小每个空格的地方增加两个空格大小,新数组 = 原数组大小+2*空格数。
代码实现:
void space_to_symbol(){ char str[100] = "talk is cheap show me the code"; cout << str << endl; char* start = str; int space_count = 0; int old_length = 0; while (*start) { if (*start == ' ') { space_count++; } old_length++; start++; } int new_length = old_length + space_count * 2; while (old_length >= 0) { if (str[old_length] == ' ') { str[new_length--] = '$'; str[new_length--] = '$'; str[new_length--] = '$'; } else { str[new_length--] = str[old_length]; } old_length--; } cout << str << endl;}int main(){ space_to_symbol(); system("pause"); return 0;}
阅读全文
1 0
- 【C++】将字符串中的空格替换成字符
- 将字符串中的字符替换成指定字符c
- 替换空格 将一个字符串中的空格替换成“ ”
- 将一个字符串中的空格替换成“ ”
- 将字符串中的空格替换成“%20”
- 将一个字符串中的空格字符替换成另外一个字符串
- 换掉字符串中的空格,将空格替换成%20
- 替换空格(将字符串中的空格替换为20%)
- 替换字符串中的空格为其他字符
- 字符串替换空格:请实现一个函数,把字符数组中的每个空格替换成“%20”。
- 字符串替换空格:请实现一个函数,把字符数组中的每个空格替换成“%20”
- 字符串替换空格:请实现一个函数,把字符数组中的每个空格替换成“%20”。
- 实现一个函数,将字符串中的空格替换成"%20"
- 将一个字符串中的空格替换成“ %20”
- 【剑指offer】将字符串中的空格替换成"%20"
- 将一个字符串中的空格全部替换成“%20”
- 将字符串中的空格用逗号替换
- 将字符串中的空格用逗号替换
- (转)Rust: Rust的 Deref 运算符
- Tomcat学习总结(7)—— Tomcat常用参数配置说明
- redis集群安装配置
- RSA--低加密指数广播攻击例子
- jsp中动态增加html模块并提交表单的几个问题
- 【C++】将字符串中的空格替换成字符
- Hadoop系列--Linux下Hadoop安装与配置
- hdu 1466
- /*Oracle添加约束*/
- 汇编语言标识
- Hadoop中HDFS相关
- http前端缓存(一)
- Spring MVC-统一异常处理
- JavaScript高级程序设计第11章-DOM扩展