输入一行字符,分别统计出其中的英文字母.空格.数字.和其他字符的个数!因为刚刚开始学想想好象很容易!我做来做去总是有错误!希望大家可以赐教!尽量不要用太复杂的方法!多谢!为什么运行的时候显示的答案不正确呢 555555555555555555
#include <stdio.h>#include <ctype.h> /*for type hceck*/#include <string.h>/*for strlen()*/int main(){ int i; unsigned letters = 0; /*字母*/ unsigned spaces = 0; /*空格*/ unsigned digits = 0; /*数字*/ unsigned others = 0; /*其他*/ char line[256]; printf("Enter a line of words: \n"); gets(line); for(i = 0; i < strlen(line); ++i) { if(isalpha(line[i])) ++letters; else if(isspace(line[i])) ++spaces; else if(isdigit(line[i])) ++digits; else ++others; } printf("The line you entered has:\n"); printf("%d letters\n", letters); printf("%d spaces\n", spaces); printf("%d digits\n", digits); printf("%d others\n", others);}
#include <stdio.h>#include <string.h>void main(){char a[1000], i, x1, x2, x3, x4;scanf("%s", a);x1 = 0; x2 = 0; x3 = 0; x4 = 0;for (i = 0; i < strlen(a); i++)if (((a[i] >='a') && (a[i] <= 'z')) || ((a[i] >='A') && (a[i] <= 'Z'))) x1 ++;else if (a[i] == ' ') x2++;else if ((a[i] >= '0') && (a[i] <= '9')) x3++;else x4++;printf("%d %d %d %d\n", x1, x2, x3, x4);}