c指针语言

来源:百度知道 编辑:UC知道 时间:2024/06/08 13:25:50
统计一字符串中各种字符的个数,用指针来做,一下我的代码错在哪里?
#include<stdio.h>
#include<conio.h>
#define N 100
void zsh(char *,int *,int *,int *,int *,int *);
int main(void)
{
int a=0,b=0,c=0,d=0,e=0,*p1=a,*p2=b,*p3=c,*p4=d,*p5=e;

char str[N],*pt=str;
printf("请输入一个字符串:\n");
gets(pt);
zsh(pt,p1,p2,p3,p4,p5);
printf("该字符串有%d个大写字母,%d个小写字母,%d个数字,%d个空格,%d个其他字符。\n",a,b,c,d,e);
getch();
return 0;
}
void zsh(char *pt,int *p1,int *p2,int *p3,int *p4,int *p5)
{
for(;*pt!=0;)
{
if((*pt>='A')&&(*pt<='Z') )
(*p1)++;
else if((*pt>='a')&&(*pt<='z'))
(*p2)++;
else if((*pt>='0')&&(*pt<='9'))
(*p3)++;

这行有问题
貌似 *p没有指向a b c d e 的地址
int a=0,b=0,c=0,d=0,e=0,*p1=a,*p2=b,*p3=c,*p4=d,*p5=e;

改成 int a=0,b=0,c=0,d=0,e=0,*p1=&a,*p2=&b,*p3=&c,*p4=&d,*p5=&e;
试试

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define N 100
void zsh(char str[],int cout[5]);
int main(void)
{
int c[5]={0};
char str[N];
printf("请输入一个字符串:\n");
gets(str);
zsh(str,c);
printf("该字符串有%d个大写字母,%d个小写字母,%d个数字,%d个空格,%d个其他字符。\n",c[0],c[1],c[2],c[3],c[4]);
getch();
return 0;
}
void zsh(char str[],int cout[])
{
int i;
for(i=0;str[i];i++)
{
if(isupper(str[i])) cout[0]++;
else if(islower(str[i])) cout[1]++;
else if(isdigit(str[i])) cout[2]++;
else if(str[i]==' ') cout[3]++;
else cout[4]++;
}
}