- #include <stdio.h>
- int main() {
-
- char str[] = "hello world! how are you?";
-
- int i = 0;
-
- while (str[i] != '\0') {
-
- if (str[i] == ' ') {
-
- for (int j = i; str[j] != '\0'; j++) {
-
- str[j] = str[j+1];
- }
- }
- i++;
- }
- printf("%s\n",str);
-
- return 0;
- }
复制代码 方法很简单,就没写注释了。主要的思想就是,从左往右,找到一个空格就将后面的字符依次前移一位,包括最后面的'\0'
|
|