samedi 27 juin 2015

Program that counts the number of types of letters in a paragraph, C++

What I want is to have a multiple-line text input, and to be able to count the number of lower-case letters, upper-case letters, periods, commas, spaces, line-breaks, and other characters in the input.

I am trying to use just one string with getline for inputs in a while loop with a running count for each punctuation category.

I just don't know how to actually figure out how many of each character type there are in each line. Given a string, how do I count the number of each type?

Here is my code so far (obviously incomplete):

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

Aucun commentaire:

Enregistrer un commentaire