c语言编程 输入一个字符串,统计其中空格的个数

如题所述

第1个回答  2013-12-19
char str[BUFSIE + 1] = {0};
int spaseCnt;

gets(str);
/*or
fgets(str,BUFSIZ, stdin);
str[ strchr(str,'\n') - str ] = '\0';
*/
spaseCnt = 0;
size_t i = 0;
while (str[i] != '\0') {
 if (isspase(str[i])) {
  ++spaseCnt;
 }
 ++i;
}
printf("空格数量:%d", spaseCnt);
第2个回答  2013-12-19
char myStr[100];
gets(myStr);
int count=0;
char* p=myStr;
while(p!='\0')
{
if(p==32)
count++;
p++;
}
printf("%d",count);本回答被网友采纳
第3个回答  2013-12-19
相似回答