永发信息网

编写一函数,由实参传递一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果(用C语言编写)

答案:2  悬赏:40  手机版
解决时间 2021-04-28 11:49
  • 提问者网友:你给我的爱
  • 2021-04-27 10:58
编写一函数,由实参传递一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果(用C语言编写)
最佳答案
  • 五星知识达人网友:佘樂
  • 2021-04-27 11:51

#include <stdio.h>


void count(char *s, int *a, int *b, int *c, int *d)
{
*a = *b = *c = *d = 0;
while(*s)
{
if('A' <= *s && *s <= 'Z' || 'a' <= *s && *s <= 'z')
(*a)++;
else if('0' <= *s && *s <= '9')
(*b)++;
else if(*s == ' ')
(*c)++;
else
(*d)++;
s++;
}
}


int main()
{
char s[100];
int lc, dc, sc, oc;
printf("输入字符串:\n");
gets(s);
count(s, &lc, &dc, &sc, &oc);
printf("字母:%d\n", lc);
printf("数字:%d\n", dc);
printf("空格:%d\n", sc);
printf("其它:%d\n", oc);
return 0;
}

全部回答
  • 1楼网友:青灯有味
  • 2021-04-27 12:44
#include <stdio.h> #include <ctype.h> void tongji( char* s, int* a, int* b, int* c, int* d ) { *a = *b = *c = *d = 0; while( *s ) { if ( isalpha( *s ) ) ++*a; else if ( isdigit( *s ) ) ++*b; else if ( isspace( *s ) ) ++*c; else ++*d; ++s; } } int main() { char s[100]; int a, b, c, d; gets( s ); tongji( s, &a, &b, &c, &d ); printf( "字母:%d\n", a ); printf( "数字:%d\n", b ); printf( "空格:%d\n", c ); printf( "其他:%d\n", d ); }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯