首页 > 试题广场 >

输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他

[问答题]
输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少。
推荐

#include<stdio.h>

int main()

{int upper=0,lower=0,digit=0,space=0,other=0,i=0;

char *p,s[20];

printf("input string:  ");

while((s[i]=getchar())!=’\n’)i++;

p=&s[0]:

while(*p!=’\n’)

{if(("A'<=*p)&&(*p<='Z’))

++upper;

else if((‘a’<=*p)&&(*p<=’z’))

++lower;

else if(*p==’ ‘)

++space;

else if((*p<=’9’)&&(*p>=’0’))

++digit

else

++other;

p++;

}

printf("upper case:%d  lower case:%d",upper,lower);

printf("  space:%d   digit:%d   other:%d\n",space,digit,other);

return 0;

}


发表于 2018-03-25 10:32:12 回复(0)
更多回答
#include <stdio.h>
int main() {
	int up = 0, low = 0, num = 0, space = 0, oth = 0, i = 0;
	char *p, s[20];
	printf("input the string:");
	while ((s[i] = getchar()) != '\n')
		i++;
	p = &s[0];
	while (*p != '\n') {
		if (('A' <= *p) && (*p <= 'Z')) {
			up++;
		}
		else if (('a' <= *p) && (*p <= 'z')) {
			low++;
		}
		else if (*p == ' ') {
			space++;
		}
		else if ((*p >= '0') && (*p <= '9')) {
			num++;
		}
		else {
			oth++;
		}
		p++;			
	}
	printf("up:%d low:%d space:%d num:%d oth:%d", up,low,space,num,oth);
}

发表于 2020-03-31 19:53:45 回复(0)
    while(s[i]=getchar()!='\n') i++;    /*getchar是一个一次只能输入一个字符的函数*/
    p=&s[0];
    while(*p!='\n')
{
    if((*p>='A')&&(*p<='z'))
    uppper++;                                                /*大写字母的计数*/
    if((*p>='a')&&(*p<='z'))
    lower++;
    if(*p= ' ')
    space++;
    if(*p>=0&&*P<=9)
    digit++;
}

发表于 2018-05-21 20:38:27 回复(0)