如果不使用stdio.h和stdlib.h以外的任何库,我就不知道如何删除句子开头的空格。
#include
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."
这段代码只留下一个空间,其中有更多的空间,但我想删除所有的空间在开始,以及在预期的输出。