PTA function (C++)

Today is also written by my boyfriend! ! !

7-11 Counting numeric characters and spaces (10 points)
This question requires writing a program to enter a line of characters, and count the number of numeric characters, spaces and other characters. It is recommended to use the switch statement to write.

Input format:
Input several characters in one line, and the last carriage return indicates the end of input, which is not counted.

Output format:
follow in one line

blank = the number of blanks, digit = the number of digit characters, other = the number
of other characters , output in the format. Note that there is a space on the left and right of the equal sign, and a space after the comma.

Input sample:
Here is a set of input. E.g:

Reold 12 or 45T
output example:
Here is the corresponding output. E.g:

blank = 3, digit = 4, other = 8

#include <iostream>

using namespace std;
#include<string>
void ccc(char abc)
{
    
    
    int a=0,b=0,c=0;
    while((abc=cin.get())!='\n')
    {
    
    
        if(abc>='0'&&abc<='9')
        {
    
    
            a++;
        }
        else if(abc==' ')
        {
    
    
            b++;
        }
        else
            c++;
    }
    cout<<"blank = "<<b<<","<<' '<<"digit = "<<a<<","<<' '<<"other = "<<c <<endl;
    
}
int main ()
{
    
    
    char abc;
    ccc(abc);
    return 0;
}

There is also a boyfriend who wrote along my line of thought~

#include <iostream>

using namespace std;
#include<string>
void ccc(char abc[],int n)
{
    
    
    int a=0,b=0,c=0;
    int i;
    for (i=0;i<n;i++)
    {
    
    
        if(abc[i]>='0'&&abc[i]<='9')
        {
    
    
            a++;
        }
        else if(abc[i]==' ')
        {
    
    
            b++;
        }
        else
        {
    
    
            c++;
        }
    }
    cout<<"blank = "<<b<<","<<" digit = "<<a<<","<<" other = "<<c<<endl;
}
int main()
{
    
    
    char abc[1000];
    char ch;
    int i;
    int n=0;
    ch=getchar();
    for(i=0;ch!='\n';i++)
    {
    
    
        abc[i]=ch;
        n++;
        ch=getchar();
    }
    ccc(abc,n);
    return 0 ;
}

Array writing, the idea is almost the same~
Insert picture description here
Thanks again boyfriend! ! ! !

Guess you like

Origin blog.csdn.net/qq_51373012/article/details/114155571