A. c语言编程:输入一行字符,输出其中英文字母、空格、数字和其它字符的个数
#include <stdio.h>
int isletter(char c)
{
return c>='a'&&c<='z'||c>='A'&&c<='Z';
}
int isdigit(char c)
{
return c>='0'&&c<='9';
}
int isblank(char c)
{
return c==' ';
}
int main()
{
char c;
int letters,digits,blanks,others;
for(letters=digits=blanks=others=0;(c=getchar())!=' ';)
if(isletter(c))
letters++;
else if(isdigit(c))
digits++;
else if(isblank(c))
blanks++;
else
others++;
printf("letters:%d blanks:%d digits:%d others:%d ",letters,blanks,digits,others);
return 0;
}
B. 输入一个字符串,统计出其中空格的个数 C语言
思路:统计字符串中的空格,所以该字符串中有空格,则输入只能使用gets函数,再依次遍历该字符串,判断字符是否是空格,如果是,则空格个数自加1。
参考代码:
#include<string.h>
#include<stdio.h>
#include<math.h>
intmain()
{
intsum=0,i;
chara[100];
gets(a);
for(i=0;a[i]!='