【问题标题】:How to code a C++ program which counts the number of uppercase letters, lowercase letters and integers in an inputted string?如何编写一个 C++ 程序来计算输入字符串中大写字母、小写字母和整数的数量?
【发布时间】:2015-05-11 00:08:10
【问题描述】:

我正在寻找一种简单而基本的方法(非常适合初学者学习最简单的方法)用 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,对吗?我们如何更改它以便我们可以在字符串中包含空格?

【问题讨论】:

  • 我们不会为您编写代码。我们解决现有代码的问题。
  • 发布您已经尝试编写的代码怎么样?
  • 请在tutorialspoint.com/cplusplus之类的网站上尝试学习c++,如果有困难,可以在这里发帖。
  • 对不起各位,我很抱歉。
  • 使用小写字母数组和大写字母数组。搜索数组。如果字母存在于数组中,则增加相应的计数器。或使用std::tolower 和std::toupper。

标签: c++ string uppercase counting lowercase


【解决方案1】:

试试:

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

【讨论】:

  • 特别是考虑到这没有解释,并且使用 lambda 表达式,我不确定它是否符合“易于新手使用/学习”的条件。
  • @BradleyDotNET - 关于这个和 other 提交,在 SO 中,为家庭作业提供过于复杂和/或危险的解决方案是一种传统,特别是如果 OP 没有尝试站在他这边... ;)
【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    相关资源
    最近更新 更多