Modified the wordcounter program, completion.

+- wordcounter.cpp
This commit is contained in:
Charles Descamps 2024-09-05 18:55:39 -07:00
parent 2508c5383e
commit 631aa69e32
2 changed files with 35 additions and 4 deletions

View file

@ -15,20 +15,51 @@ Dependencies: ???
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <list>
#include <set>
#include <unordered_map>
#include <time.h>
using namespace std;
// function prototypes
// NONE
// class definitions
// NONE
// main program
int main(int argc, char **argv)
{
int numSentences = 0;
int numSpaces = 0;
string tempString;
// ask user for number of sentences
cout << "How many sentences would you like to enter?" << '\n';
cin >> numSentences;
cout << endl;
// for loop for each sentence, count the words by counting the number of spaces, print results
for (int sentenceIterator = 0; sentenceIterator < numSentences; sentenceIterator++)
{
// ask user for sentence text
cout << "Enter text for sentence " << sentenceIterator + 1 << ": " << endl;
// ignore whitespace, put ALL whitespace into cin.
cin.ignore();
// get the input from user, dump into tempString
getline(cin, tempString);
// count number of spaces
for(int characterIterator = 0; characterIterator < tempString.size(); characterIterator++)
{
// iterate through string, count if there is space.
if(tempString[characterIterator] == ' ')
{
// count space
numSpaces = numSpaces + 1;
}
}
// add an extra 'word' because of the end.
numSpaces = numSpaces + 1;
// print out to the user
cout << "Sentence " << sentenceIterator + 1 << " has " << numSpaces << " words." << endl;
// reset word counter
numSpaces = 0;
}
// return 0
return 0;
}