爱问知识人 爱问教育 医院库

C语言程序题

首页

C语言程序题

写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果。

提交回答
好评回答
  • 2019-03-28 18:03:53
      void stat(char *str)
    {
    	int n1,n2,n3,n4;
    	n1 = n2 = n3 = n4 = 0;
    	while (*str)
    	{
    		if ((*str >= 'a' && *str = 'A' && *str = '0' && *str <= '9')
    			n2++;
    		else if (*str == ' ')
    			n3++;
    		else n4++;
    		str++;
    	}
    	printf("the string has %d letters, %d numbers, %d spaces and %d other characters。
      \n", n1, n2, n3, n4); } void main() { char str[51]; printf("please enter the string (no more than 50 characters): "); scanf("%[^\n]", str); //仅当回车时才读入整个字符串,如果用%s则会在遇到空格时就读入字符串了。
       stat(str); } 。

    唐***

    2019-03-28 18:03:53

其他答案

    2019-03-28 18:43:15
  •   对楼上的略作修改:
    #include
    #include
    void stat(char *str)
    {
    int n1,n2,n3,n4;
    n1 = n2 = n3 = n4 = 0;
    while (*str!='\0')
    {
    if ((*str >= 'a' && *str = 'A' && *str = '0' && *str <= '9')
    n2++;
    else if (*str == ' ')
    n3++;
    else n4++;
    str++;
    }
    printf("the string has %d letters, %d numbers, %d spaces and %d other characters。
      \n", n1, n2, n3, n4); } void main() { char str[51]; clrscr(); printf("please enter the string (no more than 50 characters): \n"); gets(str); stat(str); getch(); } 。
      

    e***

    2019-03-28 18:43:15

  • 2019-03-28 17:59:13
  •   #include "stdio。h"
    #include "ctype。
      h" main() { char c[256]="grw etG&&HG&^(*&^&( 9 8 7 86Hrtghh"; register int i; int a=0,n=0,s=0,o=0; for(i=0;i<256;i++) { if(c[i]=='\0') i=256; if(isalpha(c[i])) a++; else if(isdigit(c[i])) n++; else if(isspace(c[i])) s++; else o++; } printf("Alpha:\t%d\nNumber:\t%d\nSpace:\t%d\nOthers:\t%d\n",a,n,s,o); }。

    如***

    2019-03-28 17:59:13

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 176-195
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):