c语言入门基础题(非C++)

来源:百度知道 编辑:UC知道 时间:2024/05/21 18:46:00
写一个程序统计字符串中的字母和数字和其他字符的个数,输出结果.字符串输入时以回车作为结束,回车不参加统计.

用比较基础的代码写,因为我刚学C语言哈^^
谢谢了!

#include<stdio.h>
void main()
{
char ch;
int letter_sum = 0;
int digit_sum = 0;
int others_sum = 0;
printf("Please input the string:");
do
{
ch = getchar();
if((ch >= 'a'&& ch <='z') || (ch >='A'&&ch <= 'Z'))
letter_sum++;
else if(ch >= '0' && ch <= '9')
digit_sum++;
else if(ch != '\n')/*回车符不能算做其他字符,不参加统计*/
others_sum++;
}while(ch != '\n');
printf("The string has %d letters.\n",letter_sum);
printf("The string has %d digits.\n",digit_sum);
printf("The string has %d others.\n",others_sum);
}
已经测试通过,好处是不限制所要测试的字符串的长度!

#include <stdio.h>
main()
{char c;
int s=0,z=0,q=0;
for(;(c= getchar())!='\n';)
{ if(c>='0'&&c<='9')
s++;
els