C语言题目输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。

#include <stdio.h>
int main()
{
int letter=0,space=0,number=0,others=0;
char nextchar;
printf("Input your string\n");
for(;nextchar!='\n';)
{
scanf("%c",&nextchar);
if('a'<=nextchar<='z'||'A'<=nextchar<='Z')
letter++;
else if(nextchar==' ')
space++;
else if('0'<=nextchar<='9')
number++;
else
others++;
}
printf("letter=%d,space=%d,number=%d,others=%d\n",letter,space,number,others);
}

哪错啦

第1个回答  2017-10-21

问题出在while循环里的cin.get()位置不合适,换成如下即可:

#include <iostream>
using namespace std;
int main()
{
int l = 0, n = 0, o = 0;
char word;
cin.get(word);
while (word != '\n') {
if ((word >= 'a'&&word <= 'z') || (word >= 'A'&&word <= 'Z')) {
l++;
}
else if (word >= '0'&&word <= '9') {
n++;
}
else {
o++;
}
cin.get(word);
}
cout << "zimu" << l << endl << "shuzi" << n << endl << "qita" << o << endl;
}

VS2017测试通过。答题不易,正确请采纳

第2个回答  2007-06-13
#include<stdio.h>
#include<string.h>

#define MAX 100
#define LETTER 1
#define NUMBER 2
#define OTHER 3

int count(char c[], int flag)
{
int i, l=0, n=0, o=0;
for(i=0;c[i]!='\0';i++)
if(c[i]>='0'&&c[i]<='9')
n++;
else if((c[i]>='a'&&c[i]<='z')||(c[i]>='A'&&c[i]<='Z'))
l++;
else
o++;
if(flag==LETTER)
return l;
if(flag==NUMBER)
return n;
if(flag==OTHER)
return o;
}

void main()
{
char c[MAX];
printf ("input string: ");
gets (c);
printf ("\nThere are %d letters.", count(c,LETTER));
printf ("\nThere are %d numbers.", count(c,NUMBER));
printf ("\nThere are %d other characters.", count(c,OTHER));
}
第3个回答  2013-11-01
#include <iostream>
using namespace std;
#include <stdio.h>
int main(){
char x;
int digi=0,sp=0,letter=0,other=0;
while ( (x=cin.get()) != '\n'){
if (x>='0' && x <='9') digi++;
else if (x>='a' && x <='z') letter++;
else if (x>='A' && x <='Z') letter++;
else if (x == ' ') sp++;
else other++;
}
printf("letter: %d, space: %d, digi: %d, other: %d\n",letter,sp,digi,other);

return 0;
}
第4个回答  2017-09-10
即学了编程又学了英语(没学好……),岂不美哉?
(printf()函数能用那种方式是因版本的关系)

本程序的优点:不受到字符串长度的限制,运行效率高


#include <stdio.h>


int main (void)

{

//Program fuction introduce

printf ("Character statistics.\n");


//Data

char ch;

int letnum = 0;                    //The number of leters

int spanum = 0;                  //The number of space

int dignum = 0;                  //The number of digits

int resnum = 0;                  //The number of other characters


//Data input

printf ("\nPlease input a charactor string.\n");

scanf ("%c", &ch);


//Data processing

while (ch != 10)

{

if  (64 < ch && ch <91 || 96 < ch && ch < 113)

++letnum;

else if (ch == 32)

++spanum;

else if (47 < ch && ch < 58)

++dignum;

else

++resnum;


scanf ("%c", &ch);

}

++resnum;                    //Attention! Because of the newline (ASCII: 10)!


//Data output

printf ("\nThe results of data processing are as fellows.\n");

printf ("The number of letters:%8d\n"

"The number of space: %8d\n"

"The number of digits: %8d\n"

"The number of others:%8d\n",

letnum, spanum, dignum, resnum);    


//The end

printf ("\nThank you for your using!");

return 0;

}

第5个回答  2019-01-16
#include <stdio.h>
main()
{ char ch;
intletter=0,space=0,other=0;
printf("Enter a string:");
while((ch=getchar())!='\n')
{
if (ch>='A'&&ch<='Z') letter++;
else if (ch==' ') space++;
else other++;
}
printf("letter=%d, space =%d , other =%d \n", letter, space ,other);
}