【发布时间】: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++