C风格字符串左边空格移动到末尾
1、首先去除字符串左边n(n>=0)个连续空格符;
2、把去除掉的n个空格字符原数追加到去除左边空格后的字符串末尾;
3、整个过程没有改变过字符串中间含有的空格,字符串长度不变,调用函数后改变了原字符串。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <string.h> //去除字符串首部空格,并原数追加到字符串末尾 char *ltrim_rcat_sp(char *str) { if (str == NULL || *str == '') return str; int len = 0; char *p = str; while (*p == ' ') { ++p; ++len; } int iPreLen = strlen(str); memmove(str, p, iPreLen - len + 1); //调用memmove函数后改变了str指针指向内容 int iAftLen = strlen(str); for (int i = 0; i < iPreLen - iAftLen; i++) strcat(str, " "); return str; }
以此类推,可以扩展到其他字符处理。
最后
以上就是怕孤独雨最近收集整理的关于将字符串左边空格移动到末尾,不移动字符串中间空格【C/C++】C风格字符串左边空格移动到末尾的全部内容,更多相关将字符串左边空格移动到末尾内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复