How to code a C++ program which counts the number

2019-02-21 07:17发布

问题:

I'm looking for a simple and basic way (ideal for beginners to learn the easiest way) to write a program in C++ which gets a string from the user and outputs the number of uppercase letters, lowercase letters and integers (numbers). I'm pretty amateur at using C++ syntax so please help me with an easy-to-understand syntax. Thanks!

EDIT: Here is a very simple code that I found in Google and did some changes and corrections:

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

   char array1[50];
   int i = 0, lowercase = 0, uppercase = 0, numbers = 0, total;

   cout << "Enter a string: "<<endl;
   cin >> array1;
   cout <<endl;

      while (array1[i] != 0){

         if(array1[i] >= 'a' && array1[i] <= 'z'){
         lowercase++;
         i++;
       }

         else if (array1[i] >= 'A' && array1[i] <= 'Z'){
         uppercase++;
         i++;
       }

         else if (array1[i] >= '0' && array1[i] <= '9'){
         numbers++;
         i++;
       }

         else
         i++;
    }

total = lowercase + uppercase + numbers;

cout << "Your string has " << lowercase << " lowercase letters." << endl;

cout << "Your string has " << uppercase << " uppercase letters." <<endl;

cout << "Your string has " << numbers << " numbers." <<endl;

cout << "Your string has " << total << " total characters." <<endl;


getch();

return 0;
}

So in this code; we are assuming that the end of a string has the integer 0, right? How can we change it so we can have spaces in the string?

回答1:

Try:

#include <algorithm>
#include <iostream>
#include <cctype>
#include <string>

using namespace std;

int main()
{
    cout << " Enter text: ";
    string s;
    if(getline(cin, s))
    {
        size_t count_lower = count_if(s.begin(), s.end(), 
               [](unsigned char ch) { return islower(ch); });
        cout << "lowers: " << count_lower ;

        size_t count_upper = count_if(s.begin(), s.end(),    
               [](unsigned char ch) { return isupper(ch); });
        cout << "uppers: " << count_upper ;

        size_t count_digit = count_if(s.begin(), s.end(),    
               [](unsigned char ch) { return isdigit(ch); });
        cout << "digits: " << count_digit ;
    }
}


回答2:

#include<stdio.h>
main() 
{
int upper = 0, lower = 0,digit=0,special=0;
char ch[80];
int i;

printf("\nEnter The String : ");
gets(ch);

for(i = 0; ch[i]!='\0';i++)

{
if (ch[i] >= 'A' && ch[i] <= 'Z')
upper++;
else if (ch[i] >= 'a' && ch[i] <= 'z')
lower++;
else if(ch[i] >='0' && ch[i] <='9')
digit++;
else if(ch[i]!=' ')
special++;
}


printf("\nUppercase Letters : %d", upper);
printf("\nLowercase Letters : %d", lower);
printf("\nDigits : %d", digit);
printf("\nSpecial Characters : %d",special);
}


回答3:

Save as:

classify.cpp

Compile as:

g++ -o classify classify.cpp

Sample run:

$ ./classify 'Aa Bb Cc Dd Ee Ff 0123456789 - oh happy day!'
 10 7
 16 x
  6 X

Using 7 to mean any number, x any lowercase letter, and X any uppercase letter. So this input has ten digits, 16 lower case letters and six upper case letters.

Hope this code is easy to understand: just make a string and call a function. It might not work on Windows, though.

#include <cstdlib>
#include <string>

int main(int argc, char *argv[])
{
    std::string input;

    input += "echo '";
    input += argv[1];
    input += "' | sed -e 's/[a-z]/x/g' -e 's/[A-Z]/X/g' -e 's/[0-9]/7/g' -e 's/[^a-zA-Z0-9]//g' | grep -o . | sort | uniq -c";

    system(&input[0]);

    return 0;
}

The function call system is really useful, you can pass it things like "yes | cat yes" or "rm -f *.?pp".