在一行上输入一个长度为
的字符串。
第一行输出一个整数,代表字符串中英文字母的个数。
第二行输出一个整数,代表字符串中空格的个数。
第三行输出一个整数,代表字符串中数字的个数。
第四行输出一个整数,代表字符串中其它字符的个数。
1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][
26 3 10 12
#include <stdio.h>
int main() {
//也是送分题,关键是要处理好输入字符串的空格和计数初始化置0
char string[1001];
scanf("%[^\n]",string); //获取字符串知道遇到换行符,还有一个fgets()函数也可以读取有空格字符串
//printf("%s",string);
int english=0,space=0,number=0,other=0; //四种字符计数,要置0,不然初始化数值不稳定
for (int i=0; string[i]!='\0'; i++) {
if(string[i]==' ')
{
space++;
}else if (string[i]>='0'&&string[i]<='9') {
number++;
}else if (string[i]>='a'&&string[i]<='z'||string[i]>='A'&&string[i]<='Z') {
english++;
}else {
other++;
}
}
printf("%d\n",english);
printf("%d\n",space);
printf("%d\n",number);
printf("%d\n",other);
return 0;
} #include <stdio.h>
#include<string.h>
int main() {
char word[1001];
char c;
int i = 0;
while ((c = getchar()) != EOF) { // 注意 while 处理多个 case
word[i] = c;
i++;
}
int len = i;
int letter = 0, space = 0, num = 0, other = 0;
for (i = 0; i < len; i++) {
if (word[i] == ' ')space++;
else if (word[i] >= '0' && word[i] <= '9')num++;
else if ((word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' &&
word[i] <= 'Z'))letter++;
else if (word[i] != '\n')other++;
}
printf("%d\n%d\n%d\n%d", letter, space, num, other);
return 0;
} 注意处理最后的换行符
#include <ctype.h>
#include <stdio.h>
int main() {
char c;
int cnt_alpha = 0;
int cnt_num = 0;
int cnt_blank = 0;
int cnt_oth = 0;
while ((c = getchar()) != EOF) {
if(isalpha(c)){
cnt_alpha++;
}else if(isdigit(c)){
cnt_num++;
}else if(isblank(c)){
cnt_blank++;
}else if('\n' == c){
continue;
}else{
//printf("[%c]\n",c);
cnt_oth++;
}
}
printf("%d\n", cnt_alpha);
printf("%d\n", cnt_blank);
printf("%d\n", cnt_num);
printf("%d\n", cnt_oth);
return 0;
} #include <stdio.h>
# include<string.h>
int main() {
int a[4] = {0};char s[1001] = {'\0'};
gets(s);
for(int i = 0;s[i] != '\0'; i++){
if(islower(s[i]) || isupper(s[i])) a[0]++;
else if(s[i] == 32) a[1]++;
else if(47<s[i] && s[i]<58) a[2]++;
else a[3]++;
}
for(int i = 0; i<4; i++) printf("%d\n",a[i]);
return 0;
} #include <stdio.h>
int main(void)
{
char str[1000], *p, word[128] = {0};
int e = 0, d = 0, s = 0, o = 0, i;
while(scanf("%s,", str) != EOF)
{
p = str;
do
{
word[*p]++;
o++;
}while(*(++p));
word[0]++;
}
for(i = 'A'; i <= 'Z'; i++) e += word[i] + word[i + 32];
for(i = '0'; i <= '9'; i++) d += word[i];
printf("%d\r\n%d\r\n%d\r\n%d", e, word[0] - 1, d, o - e - d);
} #include <stdio.h>
#define N 1000
int main()
{
char str[N];
int letter=0,space=0,number=0,other=0,i=0;
gets(str);
while(str[i]!='\0')
{
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
{
letter++;
}
else if(str[i]==' ')
{
space++;
}
else if(str[i]>='0'&&str[i]<='9')
{
number++;
}
else
{
other++;
}
i++;
}
printf("%d\n%d\n%d\n%d\n",letter,space,number,other);
return 0;
} #include<stdio.h>
#include<string.h>
int main() {
char str[1001] = {'\0'};
while (gets(str)) {
int len = strlen(str);
int charac = 0, blank = 0, num = 0, other = 0;
for (int i = len - 1; i >= 0; i--) {
//字母计数(区分大小写)
if ((('a' <= str[i])&&(str[i] <= 'z')) || (('A' <= str[i])&&(str[i] <= 'Z'))) charac++;
else if (str[i] == ' ') blank++; //空格计数
else if (('0' <= str[i]) && (str[i] <= '9')) num++; //数字计数
else other++; //其他字符计数
}
printf("%d\n%d\n%d\n%d\n", charac, blank, num, other);
}
} #include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char str[256] = {0};
char *p = NULL;
int space = 0;
int alpha = 0;
int num = 0;
int eth = 0;
gets(str);
p = str;
while (*p != '\0')
{
if(*p == 32)
{
space++;
}
else if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z'))
{
alpha++;
}
else if (*p >= 48 && *p <= 57)
{
num++;
}
else
{
eth++;
}
p++;
}
printf("%d\n%d\n%d\n%d\n", alpha, space, num, eth);
return 0;
} 无难度的题
#include<stdio.h>
int main(){
char in[500];
while (gets(in)) {
int yw=0, kg=0, sz=0, qt=0;
for (int i = 0; i < strlen(in); i++) {
if (in[i] == ' ') {
kg++;
} else if ((in[i] >= 'A' && in[i] <= 'Z') || in[i] >= 'a' && in[i] <= 'z') {
yw++;
} else if (in[i] >= '0' && in[i] <= '9') {
sz++;
} else {
qt++;
}
}
printf("%d\n%d\n%d\n%d\n", yw, kg, sz, qt);
}
}