【问题标题】:Program that counts the number of types of letters in a paragraph, C++计算段落中字母类型数量的程序,C++
【发布时间】:2015-06-25 22:53:20
【问题描述】:

我想要的是有一个多行文本输入,并且能够计算小写字母、大写字母、句点、逗号、空格、换行符和其他字符的数量输入。

我试图在一个while循环中只使用一个带有getline的字符串作为输入,每个标点符号类别都有一个运行计数。

我只是不知道如何实际计算出每行中每种字符类型的数量。给定一个字符串,如何计算每种类型的个数?

这是我目前的代码(显然不完整):

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;

int main(){
    cout << "This program takes any number of sentences as inputs. " << endl;
    cout << "It will count the number of lower-case letters and upper-case letters. " << endl;
    cout << "It will also count the number of periods, exclamation marks, spaces, end-lines, etc. " << endl;
    cout << " " << endl;
    cout << "Please type your text, pressing enter whenever you wish to end a line. " << endl;
    cout << "Use the EOF key (CTRL + Z on Windows) when you are finished. " << endl;

    string InputString; // This is the string that will be used iteratively, for each line.

    int NumberOfLowerCase = 0;
    int NumberOfUpperCase = 0;
    int NumberOfSpaces = 0;     // spaces
    int NumberOfTabs = 0;       // tabs
    int NumberOfPeriods = 0;    // periods
    int NumberOfCommas = 0;     // commas
    int NumberOfOtherChars = 0; // other characters
    int NumberOfEnters = 0;     // end of line, will be incremented each loop

    do {
        getline(cin, InputString);   // input
        cout << InputString << endl; // filler just to test the input 
        NumberOfLowerCase = NumberOfLowerCase + 0   // I don't know what I should be adding
                                                    // (obviously not zero, that's just a filler)
    } while (!cin.eof() && cin.good());

    system("pause");
    return 0;
}

【问题讨论】:

  • 检查开关或 if 语句中的每个字符。相应地增加相应的计数器。
  • 我没有对你投反对票,但有许多问题已经解决了这个问题,而且可能还有很多网络资源。这是一个:stackoverflow.com/questions/28948722/…

标签: c++


【解决方案1】:

如果您只是想要唯一字符的数量,请使用set!您可以将所有角色推入场景中,然后只需检查场景有多大就可以了!

如果你真的想知道每个字符有多少,你可以使用map(实际上它在后台使用了一个集合!)。有了地图,给定一些字符c,你可以这样做

std::map<char, int> counter;
//do stuff...
counter[c]++; //increment the number of character c we've found
//do more stuff...
std::cout << "Found " << counter['A'] << " A's!" << std::endl;

【讨论】:

  • @TerrenceTown 如果这是您要寻找的答案,请accept it。如果没有,请告诉我您还想知道什么!
  • 好吧,我不太明白,但我可以告诉你知道你在做什么:D
  • @TerrenceTown 你在哪里困惑?让我知道,以便我在答案中解释更多!
  • @scohe001,由于不需要std::map 的排序,如果OP 使用大段落,使用std::unordered_map 可以更有效地解决此问题
【解决方案2】:

请参阅these 有用的功能。在这里你会做什么:

std::string s = /*...*/;

for(auto c : s) {
    if(std::islower(c))      ++NumberOfLowerCase;
    else if(std::isupper(c)) ++NumberOfUpperCase;
    else if(c == ' ')        ++NumberOfSpaces;
    else if(c == '\t')       ++NumberOfTabs;
    else if(c == '.')        ++NumberOfPeriods;
    else if(c == ',')        ++NumberOfCommas;
    else                     ++NumberOfOtherChars;
}

【讨论】:

    【解决方案3】:

    这是我很快写的一个非常简单的例子。当然有更好的方法,但这应该让您了解如何做到这一点。一个问题:您是直接从控制台读取文件还是从 istream 读取?

    int lowerCase = 0;
    int upperCase = 0;
    int spaces = 0;     // spaces
    int tabs = 0;       // tabs
    int newLines = 0;   // end of line, will be incremented each loop
    int periods = 0;    // periods
    int commas = 0;     // commas
    int otherChars = 0;
    
    // read from istream, char by char
    for (char ch; cin >> noskipws >> ch;) {
        // test which classification or char ch is and increment its count
        if (islower(ch))
            ++lowerCase;
        else if (isupper(ch))
            ++upperCase;
        else if (ch == ' ')
            ++spaces;
        else if (ch == '\t')
            ++tabs;
        else if (ch == '\n')
            ++newLines;
        else if (ch == '.')
            ++periods;
        else if (ch == ',')
            ++commas;
        else
            ++otherChars;
    }
    
    cout << "Number of characters of each type:\n";
    
    cout << "lowerCase:\t" << lowerCase << '\n'
         << "upperCase:\t" << upperCase << '\n'
         << "spaces:\t\t" << spaces << '\n'
         << "tabs:\t\t" << tabs << '\n'
         << "periods:\t" << periods << '\n'
         << "commas:\t\t" << commas << '\n'
         << "newLines:\t" << newLines << '\n'
         << "otherChars:\t" << otherChars << '\n';
    

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 1970-01-01
      • 2014-02-26
      • 2021-04-01
      • 2014-07-22
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2017-06-05
      相关资源
      最近更新 更多