替换字符串中的空格
来源:互联网 发布:网络招商怎么精准找到 编辑:程序博客网 时间:2024/05/18 03:10
//替换字符串中的空格#include<iostream>#include<stdlib.h>using namespace std;void ReplaceBlank(char *arr, int capacity)//capacity为数组arr的总容量{ if (arr == NULL || capacity <= 0) return; int BlankCount = 0;//空格数 int size = 0;//数组的大小 int idx = 0; while (arr[idx] != '\0')//计算空格以及数组的大小 { size++; if (arr[idx] == ' ') BlankCount++; idx++; } int newSize = size + BlankCount * 2;//替换空格后数组的大小 if (newSize > capacity) return; //从后往前寻找并替换空格 int idx_old = size;//idx_old指向原字符串的末尾arr[size]的位置 int idx_new = newSize;//idx_new指向替换后的字符串的末尾arr[newSize]的位置 while (idx_new>idx_old && idx_old>=0) { if (arr[idx_old] != ' ') { arr[idx_new] = arr[idx_old]; idx_new--; } else { arr[idx_new--] = '$'; arr[idx_new--] = '$'; arr[idx_new--] = '$'; } idx_old--; }}int main(){ char arr[16] = "abc def ghi"; cout<<sizeof(arr)<<endl;//16 ReplaceBlank(arr, 16); for (size_t i = 0; i < strlen(arr); ++i) { cout << arr[i] << " "; } cout << endl; system("pause"); return 0;}
阅读全文
0 0
- 替换字符串中的空格
- 替换字符串中的空格
- 替换字符串中的空格
- 替换字符串中的空格
- 替换字符串中的空格
- 替换字符串中的空格
- 字符串中的替换空格
- 替换字符串中的空格
- 字符串中的空格替换
- 替换字符串中的空格
- 字符串中的空格替换
- 替换字符串中的空格
- 替换字符串中的空格
- 替换字符串中的空格
- 替换字符串中的空格
- 替换字符串中的空格
- 替换字符串中的空格
- 替换字符串中的空格
- eclipse下如何导入和删除jar包
- learning之策略模式
- java断言
- day07之包含min函数的栈+查找一个字符串中第一个只出现两次的字符
- 史上难得的自定义效果——屏幕中随机出来十个头像不重叠
- 替换字符串中的空格
- Java中使用Jedis操作Redis
- Jenkins SDK does not exist for mac
- 第四次零售革命离不开零售大数据分析
- [JavaSE笔记]Java中易被忽视的一些问题
- java生成数字001到999的2种方法
- Ueditor无法回显html
- Python Cookbook(第2版)中文版
- C++ 结构体在内存中的对齐规则