西南科技大学Power OJ:实验五 J: 课本第六章-10 统计数组中各类字符的个数

Description

有一篇文章,共n行文字,每行最多80个字符,要求分别统计出其中英文大写字母、小写字母、数字、空格、以及其他字符的个数。
Input
第一行,输入一个正整数n
(1≤n≤100),表示文章的行数。
接下来n
行,每行一个字符串,表示文章的一行。每行最多80个字符。
Output
输出5个正整数,用空格隔开,以换行符结尾,分别为该文章中英文大写字母、小写字母、数字、空格、以及其他字符的个数。

**Sample Input**
Raw

3
[email protected]
https://www.oj.swust.edu.cn
hello~

**Sample Output**
Raw

0 30 12 0 10
Hint
此题可用于练习二维字符数组,提醒大家,在输入了n后,回车,而这个回车,可用一个gets()函数来接收。

    int n;    
    char s[100][100];
    scanf("%d", &n);
    gets(s[0]);
    for(i = 0 ; i < n ; i ++)
        gets(s[i]);

示例代码:

#include<stdio.h>
int main()
{
    
    
	char str[101][81];
	int i,j;
	int n;
	scanf("%d",&n);
	gets(str[0]);
	for(j=0;j<n;j++)
    	gets(str[j]);
	int a=0, b=0, c=0, d=0, e=0;
	for(j=0;j<n;j++)
	{
    
    
		for(i=0;i<81&&(str[j][i]!='\0');i++)
		{
    
    
		  if(str[j][i]== ' ')
		{
    
    
          d++;
		}
        else if(str[j][i] >= 'a' &&  str[j][i] <= 'z')
		{
    
    
           b++;
		}
        else if(str[j][i] >= 'A' && str[j][i] <= 'Z')
		{
    
    
          a++;
		}
	    else if (str[j][i] >= '0' && str[j][i] <= '9')
		{
    
    
			c++;
		}
        else
		{
    
    
          e = ++e;
		}
		}
	}
	printf("%d %d %d %d %d\n",a,b,c,d,e);
return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_45281807/article/details/111463612
今日推荐