1.统计各种字符的个数
/*Description
有一行字符,分别统计出该行字符中大写字母、小写字符、数字字符、空格和
其它字符的个数。
Input
测试数据有多组,每组单独占一行,包含若干字符,但不超过100个。
Output
对于每组测试数据,分别输出大写字母、小写字母、数字字符、空格和其它字符的
个数,每组输出单独占一行,数据之间用一个空格分隔。
Sample Input
ABCxy 876;'.?
mnp, . ? 98\
Sample Output
3 2 3 4 4
0 3 2 3 4*/
#define N 2
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i,j,d,x,s,k,o;
char c[N][100];
char (*p)[100];
p = c;
for(i = 0;i < N;i++)
{
gets(*(p + i));
}
for(i = 0;i < N;i++)
{
d = x = s = k = o = 0;
for(j = 0;j < strlen(*(p + i));j++)
{
if(islower(*(*(p + i) + j)))
x++;
else if(isupper(*(*(p + i) + j)))
d++;
else if(isdigit(*(*(p + i) + j)))
s++;
else if(isspace(*(*(p + i) + j)))
k++;
else
o++;
}
printf("%d %d %d %d %d\n",d,x,s,k,o);
}
}
2.统计单词个数
这个地方,我第一次想到的是用统计出来的空格的个数再加一就是单词的个数,但是如果输入多个空格就不能统计了。
这是统计空格加一
/*Description
给出一个英文句子(不超过200个字符),统计其中的单词个数。已知单词之间
用空格分隔,若有标点符号则紧跟单词,不算是单词,标点符号后有空格。
Input
测试数据有多组,每组单独占一行,只包括字符、标点符合和空格,
不含其它符号。
Output
对于每组测试数据,输出单词个数。
Sample Input
I love China!
I am a student.
Sample Output
3
4*/
#define N 2
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char c[N][200];
char (*p)[200];
p = c;
int i,j,k;
for(i = 0;i < N;i++)
gets(*(p + i));
for(i = 0;i < N;i++)
{
k = 1;
for(j = 0;j < strlen(*(p + i));j++)
{
if(isspace(*(*(p + i) + j)))
k++;
}
printf("%d\n",k);
}
}
这是输入多个空格的情况
/*Description
给出一个英文句子(不超过200个字符),统计其中的单词个数。已知单词之间
用空格分隔,若有标点符号则紧跟单词,不算是单词,标点符号后有空格。
Input
测试数据有多组,每组单独占一行,只包括字符、标点符合和空格,
不含其它符号。
Output
对于每组测试数据,输出单词个数。
Sample Input
I love China!
I am a student.
Sample Output
3
4*/
#define N 2
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char c[N][200];
char (*p)[200];
p = c;
int i,j,k,flag;
for(i = 0;i < N;i++)
gets(*(p + i));
for(i = 0;i < N;i++)
{
k = 0;
for(j = 0;j < strlen(*(p + i));)
{
flag = 0;
while(isalpha(*(*(p + i) + j)))
{
j++;
flag = 1;
}
if(flag == 1)
k++;
while(isspace(*(*(p + i) + j)))
{
j++;
}
}
printf("%d\n",k);
}
}