Я хочу иметь многострочный ввод текста и иметь возможность подсчитывать количество строчных букв, прописных букв, периодов , запятые, пробелы, разрывы строк и другие символы на входе.Программа, которая подсчитывает количество типов букв в абзаце, C++
Я пытаюсь использовать только одну строку с getline для входов в цикле while с количеством запусков для каждой категории препинания.
Я просто не знаю, как на самом деле выяснить, сколько из каждого типа символов есть в каждой строке. Учитывая строку, как я могу подсчитать количество каждого типа?
Вот мой код до сих пор (очевидно, неполным):
#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;
}
Почему downvotes? – terrace
Проверьте каждый символ в коммутаторе или с помощью if-утверждений. Постройте соответствующий счетчик соответственно. – Matt
Я не проголосовал за вас, но есть ряд вопросов, которые уже затрагивают это, и, вероятно, много ресурсов в Интернете. Вот один из них: http://stackoverflow.com/questions/28948722/how-to-code-ac-program-which-counts-the-number-of-uppercase-letters-lowercas – DigitalNinja