【发布时间】:2019-09-14 19:58:15
【问题描述】:
如果不使用 stdio.h 和 stdlib.h 以外的任何库,我无法弄清楚如何删除句子开头的空格。
#include <stdio.h>
int main()
{
char text[1000], result[1000];
int c = 0, d = 0;
printf("Enter some text\n");
gets(text);
while (text[c] != '\0') { // till the end of the string
if (text[c] == ' ') {
int temp = c + 1;
if (text[temp] != '\0') {
while (text[temp] == ' ' && text[temp] != '\0') {
if (text[temp] == ' ') {
c++;
}
temp++;
}
}
}
result[d] = text[c];
c++;
d++;
}
result[d] = '\0';
printf("Text after removing blanks\n%s\n", result);
return 0;
}
这段代码删除了句子中所有多余的空格。
示例:
输入: " this is my program."
输出: " this is my program."
预期输出: "this is my program."
这段代码只留下一个空格,还有更多空格,但我想删除开头的所有空格,就像预期的输出一样。
【问题讨论】:
-
您是否要删除字符串中的所有空格?或者您是否要删除单词之间的所有多余空格?你能提供“这是我的程序”的预期输出吗?输入?
-
我更新了,想去掉单词之间所有多余的空格,还要去掉句首的空格。