输入一行文字,用指针方式求其中的大写字母、小写字母、空格和数字及其他字符各有多少?

#include<stdio.h>
void fun(char *p,int *k1,int *k2,int *k3,int *k4,int *k5)
{
int i;
for(i=0;*(p+i)!='\0';i++)
{
if(*(p+i)>='A'&&*(p+i)<='Z')
*k1++;
else if(*(p+i)>='a'&&*(p+i)<='z')
*k2++;
else if(*(p+i)>='0'&&*(p+i)<='9')
*k3++;
else if(*(p+i)==' ')
*k4++;
else
*k5++;
}
}
void main()
{
int k1=0,k2=0,k3=0,k4=0,k5=0;
char s[100];
printf("input the data:\n");
scanf("%s",s);
fun(s,&k1,&k2,&k3,&k4,&k5);
printf("大写字母:%d\n小写字母:%d\n数字:%d\n空格:%d\n其它:%d\n",k1,k2,k3,k4,k5);
}
能运行,但输出全为0,求解

#include<stdio.h>
void fun(char *p,int &k1,int &k2,int &k3,int &k4,int &k5)
{
int i;
for(i=0;*(p+i)!='\0';i++)
{
if(*(p+i)>='A'&&*(p+i)<='Z')
k1++;
else if(*(p+i)>='a'&&*(p+i)<='z')
k2++;
else if(*(p+i)>='0'&&*(p+i)<='9')
k3++;
else if(*(p+i)==' ')
k4++;
else
k5++;
}
}
void main()
{
int k1=0,k2=0,k3=0,k4=0,k5=0;
char s[100];
printf("input the data:\n");
gets(s);
fun(s,k1,k2,k3,k4,k5);
printf("大写字母:%d\n小写字母:%d\n数字:%d\n空格:%d\n其它:%d\n",k1,k2,k3,k4,k5);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-12-07
把*k1++这种地方改成(*k1)++

++编译优先度高过*

此程序无法得到空格计数。