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]!='