如何编写一个C ++程序来计算输入字符串中的大写字母,小写字母和整数的数量?

时间:2015-03-09 17:58:29

标签: c++ string uppercase counting lowercase

我正在寻找一种简单而基本的方法(非常适合初学者学习最简单的方法)用C ++编写程序,该程序从用户获取字符串并输出大写字母,小写字母和整数(数字) )。我非常喜欢使用C ++语法,所以请用一种易于理解的语法帮助我。谢谢!

编辑: 这是我在Google中找到的一个非常简单的代码,并进行了一些更改和更正:

#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;
}

所以在这段代码中;我们假设一个字符串的结尾有整数0,对吗?我们如何改变它,以便我们可以在字符串中有空格?

3 个答案:

答案 0 :(得分:5)

尝试:

#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 ;
    }
}

答案 1 :(得分:1)

#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);
}

答案 2 :(得分:-2)

另存为:

classify.cpp

编译为:

g++ -o classify classify.cpp

示例运行:

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

使用7表示任意数字,x任何小写字母,X表示任何大写字母。因此,此输入有十位数字,16个小写字母和六个大写字母。

希望这段代码易于理解:只需创建一个字符串并调用一个函数即可。但它可能无法在Windows上运行。

#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;
}

函数调用system非常有用,您可以传递"yes | cat yes""rm -f *.?pp"之类的内容。