当我读入一个字符串的时候,怎么去掉开始和末尾的空格?

yiwenshuang 2009-06-14 01:37:33
当我读入一个字符串的时候,怎么去掉开始和末尾的空格?我要统计其单词数目,怎么改啊?

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100];
int k;

printf("\nPlease enter a sentence: \t\n\n\n\t\t\t\t");
gets(str); //读一个字符串
fflush(stdin);


//统计空格个数
for (k = 0 ; str[k] != '\0' ; k++)
{
if (str[k] == ' ')
{
space++;
}
}
}
...全文
215 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yiwenshuang 2009-06-14
  • 打赏
  • 举报
回复
好了,好了,非常感谢
tanwan 2009-06-14
  • 打赏
  • 举报
回复
你说前面有一个空格是指这样么?" Welcome to"
这样的话你可以先一个while(str[i++]==' ');
接下来就直接for(;i<len;i++)
在如果是最后一个空格则可以while(str[--i]==' ')space--;
LZ可以试试~
code_zhang 2009-06-14
  • 打赏
  • 举报
回复
刚刚貌似有点理解错楼主的意思了 楼主是要统计一个字符串里面的单词数目是吧 那么按空格来统计的话就有很多BUG了 譬如头尾的空格也会被统计进去 中间多输入的空格也会统计进去 使得结果不准确 所以不能单以空格做为统计的依据。

因把
if (str[k] == ' ')
改为
if (str[k] == ' '&&str[k+1]!=' '&&str[k+1]!='\0')
/*当第K位字符为空格,K+1位的字符不为空格或者'\0'时,space++*/

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100];
int k,space=0;

printf("\nPlease enter a sentence: \t\n\n\n\t\t\t\t");
gets(str);
fflush(stdin);
for (k = 0 ; str[k] != '\0' ; k++)
{
if (str[k] == ' '&&str[k+1]!=' '&&str[k+1]!='\0')
{
space++;
}
}
printf("%d\n",space);
}

这样就可以解决楼主的问题了。
飞天御剑流 2009-06-14
  • 打赏
  • 举报
回复
[Quote=引用楼主 yiwenshuang 的帖子:]
当我读入一个字符串的时候,怎么去掉开始和末尾的空格?我要统计其单词数目,怎么改啊?

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100];
int k;

printf("\nPlease enter a sentence: \t\n\n\n\t\t\t\t");
gets(str); //读一个字符串
fflush(stdin);


//统计空格个数…
[/Quote]

统计完后检查一下头和尾是否有空格,有的话space减掉就行啦,在循环后加两条:

for (k = 0 ; str[k] != '\0' ; k++)
{
if (str[k] == ' ')
{
space++;
}
}
if( *str == ' ' ) --space;
if( str[k] == ' ' ) --space;
code_zhang 2009-06-14
  • 打赏
  • 举报
回复
运行后显示为:
Please enter a sentence:


one two three four
4
Press any key to continue
code_zhang 2009-06-14
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100];
int k,space=1;

printf("\nPlease enter a sentence: \t\n\n\n\t\t\t\t");
gets(str);
fflush(stdin);


for (k = 0 ; str[k] != '\0' ; k++)
{
if (str[k] == ' ')
{
space++;
}
}
printf("%d\n",space);
}
只要定义一个int space=1;输出时添加一个printf("%d\n",space);就可以了。
yiwenshuang 2009-06-14
  • 打赏
  • 举报
回复
????有一个ERRO
localxiao 2009-06-14
  • 打赏
  • 举报
回复
[code]/*cout<<space<<endl;*/
printf("%n",space);
[/codee]

替换一下就可以了- -
yiwenshuang 2009-06-14
  • 打赏
  • 举报
回复
不能用C++啊
Oceanex 2009-06-14
  • 打赏
  • 举报
回复
上面哥们说的没错,只是space初始值要定义为1.。。可以验证一下的、
  • 打赏
  • 举报
回复

Please enter a sentence:


he l o
2
Press any key to continue



int main()
{
char str[100];
int k,space=0;

printf("\nPlease enter a sentence: \t\n\n\n\t\t\t\t");
gets(str); //读一个字符串
fflush(stdin);


//统计空格个数
for (k = 0 ; str[k] != '\0' ; k++)
{
if (str[k] == ' ')
{
space++;
}
}
cout<<space<<endl;
}

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧