18067 字符统计时间限制:1000MS 代码长度限制:10KB 题型: 填空题 语言: G++;GCC;VC Description编写一个函数,统计一个字符串中字母、数字和空格的个数。使用全局变量存放字母和数字个数,函数返回值是空格个数 #include <stdio.h> #include <string.h> #include <math.h> int nL=0, nN=0; int statistics(char *s) { _______________________ } int main() { char s[81]; int nS; gets(s); nS = statistics(s); printf("%d %d %d\n", nL, nN, nS); return 0; } 输入格式输入一行字符,以'\n'符结束 输出格式统计结果 输入样例Ab 45 输出样例2 2 1 |
#include <stdio.h>
#include <string.h>
#include <math.h>
int nL=0, nN=0;
int statistics(char *s)
{
int n=0;
while(*s!='\0')
{
if(*s>='a'&&*s<='z'||*s>='A'&&*s<='Z')
nL++;
if(*s>='0'&&*s<='9')
nN++;
if(*s==' ')
n++;
s++;
}
return n;
}
int main()
{
char s[81];
int nS;
gets(s);
nS = statistics(s);
printf("%d %d %d\n", nL, nN, nS);
return 0;
}