1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C语言数组字符串清除空格 用C语言从字符串中删除空格

C语言数组字符串清除空格 用C语言从字符串中删除空格

时间:2021-03-24 10:25:07

相关推荐

C语言数组字符串清除空格 用C语言从字符串中删除空格

如果不使用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."

这段代码只留下一个空间,其中有更多的空间,但我想删除所有的空间在开始,以及在预期的输出。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。