1 solutions

  • 0
    @ 2025-11-5 15:32:24

    C :

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	char s[100],*p=s;
    	int letter,spaces,number,other;
    	gets(s);
    	while(*p!='\0')
    	{
    		if((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z'))
    			letter++;
    		else if(*p>='0'&&*p<='9')
    			number++;
    		else if(*p==' ')
    			spaces++;
    		else
    			other++;
    		p++;
    	}
    	printf("%d %d %d %d\n",letter,number,spaces,other);
    }
    
    

    C++ :

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int main()
    {
    	char c;
    	int letter=0,digit=0,space=0,other=0;
    	while ((c=getchar()) != '\n')
    		if (isalpha(c)) letter++;
    		else if (isalnum(c)) digit++;
    		else if (c==' ') space++;
    		else other++;
    	cout<<letter<<" "<<digit<<" "<<space<<" "<<other<<endl;
    	return 0;
    }
    
    • 1

    C语言程序设计教程(第三版)课后习题6.2

    Information

    ID
    16903
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By