c语言求输入字符串的字母,数字,空格,其他字符的个数我弄出了下面的但是当数字字母空格在最后时就会错

如题所述

/*
请输入字符串:wwwrqr32
232tghthr
fgegtqerg{}][)(><,.?/
该字符串中含有:
小写字母:21个。
数字:5个。
空格:4个。
其他字符:12个。
Press
any
key
to
continue
*/
#include
#include
int
main
()
{
char
capitals
=
0,lowercases
=
0,digits
=
0;
char
spaces
=
0,other
=
0,length,i,c,s[200];
printf("请输入字符串:");
gets(s);
length
=
strlen(s);
for
(i
=
0;i
<
length;i++)
{
c
=
*(s
+
i);
if((c
>=
'A')&&(c
<=
'Z'))
{
capitals++;
continue;
}
if((c
>=
'a')&&(c
<=
'z'))
{
lowercases++;
continue;
}
if((c
>=
'0')&&(c
<=
'9'))
{
digits++;
continue;
}
if(c
==
'
')
{
spaces++;
continue;
}
else
other++;
}
printf("\n该字符串中含有:\n\n");
if
(capitals
>
0)
printf("大写字母:%d个。\n",capitals);
if
(lowercases
>
0)
printf("小写字母:%d个。\n",lowercases);
if
(digits
>
0)
printf("数字:%d个。\n",digits);
if
(spaces
>
0)
printf("空格:%d个。\n",spaces);
if
(other
>
0)
printf("其他字符:%d个。\n",other);
printf("\n\n");
return
0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-06-16
用c函数,
isdigit判断是否是数字,isalpha判断是否为字母,在ctype.h文件里面,还有其他判断函数。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网