录入快递单的是真的吗:这道c语言题目怎么做?

来源:百度文库 编辑:中科新闻网 时间:2024/05/10 11:35:46
输入一行字符,分别统计出其中英文字母,空格,数字和其它字符的个数。

 
 
 
可以这样做:

#include<stdio.h>
#include<ctype.h>

int main( ) {
    char buf[ 999 ],
         *c;
    int nAlpha = 0,
        nSpace = 0,
        nDigit = 0,
        nOther = 0;

    puts( "Enter the string to be analyzed statistically:" );
    gets( buf );

    for( c = buf; *c; ++c )
        isalpha( *c ) ? ++nAlpha:
        isspace( *c ) ? ++nSpace:
        isdigit( *c ) ? ++nDigit:
                        ++nOther;

    printf( "\n"
            "Character type    Count\n"
            "==============    =====\n"
            "Alphabet             %d\n"
            "Space                %d\n"
            "Digit                %d\n"
            "Other                %d\n",

            nAlpha, nSpace, nDigit, nOther );
}
 
 
 

#include <iostream.h>
#include <ctype.h>

const long MAX = 1000;

int main()
{
char str[MAX];
long digitNumber=0, letterNumber=0, blankNumber=0, otherCharNumber=0;
long loopCount;

while(cin.getline(str,MAX), cin.rdstate() == cin.goodbit)
{
for(loopCount = 0; str[loopCount] != '\0'; loopCount ++)
{
if(isalpha(str[loopCount]) != 0)
letterNumber ++;
else if(isdigit(str[loopCount]) != 0)
digitNumber ++;
else if(str[loopCount] == ' ')
blankNumber ++;
else
otherCharNumber ++;
}
}

cout << "There are " << letterNumber << " letters!" << endl
<< "There are " << digitNumber << " digits!" << endl
<< "There are " << blankNumber << " blanks!" << endl
<< "There are " << otherCharNumber << " other characters!" << endl;
return 0;
}