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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28948722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:07:12  来源:igfitidea点击:

How to code a C++ program which counts the number of uppercase letters, lowercase letters and integers in an inputted string?

c++stringuppercasecountinglowercase

提问by Sepehr Student

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!

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

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

编辑:这是我在 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;
}

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?

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

回答by mnshahab

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

回答by Arjun S Kumar

#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]!='##代码##';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);
}