该程序实现从键盘输入一个字符串,统计其中包含的空格和非空格字符的数量.要求用指针实现,急求

如题所述

下面的程序先输入一个字符串,以“回车”结束输入,定义一个字符型指针变量p,指向字符串,循环统计字符串中的空格和非空格的数量。我跑过这个程序了,没有错误的。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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;
}

运行结果如下,也可以自己跑一下程序。

温馨提示:答案为网友推荐,仅供参考