C语言 对一个字符串进行字符统计,分别统计包括空格和不包括空格的字符数

如题所述

第1个回答  推荐于2016-08-09
int main()
{
char str[50];
int i;
char * p;
int num_space;
int num_not_space;

printf("please input a string:\n");
str[0]=getchar();
i=0;
while(str[i]!='\n')
{
i++;
str[i]=getchar();
}
str[i]='\0';
printf("the string is %s\n",str);
p=str;
num_space=0;
num_not_space=0;
for(p;*p!='\0';p++)
{
if(*p==' ')
{
num_space++;
}
else
{
num_not_space++;
}
}
printf("the number of space is %d\n",num_space);
printf("the number of no space is %d\n",num_not_space);
return 0;
}本回答被提问者采纳
第2个回答  推荐于2016-08-09
int main()
{
char str[50];
int i;
char * p;
int num_space;
int num_not_space;

printf("please input a string:\n");
str[0]=getchar();
i=0;
while(str[i]!='\n')
{
i++;
str[i]=getchar();
}
str[i]='\0';
printf("the string is %s\n",str);
p=str;
num_space=0;
num_not_space=0;
for(p;*p!='\0';p++)
{
if(*p==' ')
{
num_space++;
}
else
{
num_not_space++;
}
}
printf("the number of space is %d\n",num_space);
printf("the number of no space is %d\n",num_not_space);
return 0;
}本回答被提问者采纳
相似回答
大家正在搜