#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);
}
哪错啦
问题出在while循环里的cin.get()位置不合适,换成如下即可:
#include <iostream>VS2017测试通过。答题不易,正确请采纳
#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;
}