Modified the wordcounter program, completion.
+- wordcounter.cpp
This commit is contained in:
parent
2508c5383e
commit
631aa69e32
2 changed files with 35 additions and 4 deletions
Binary file not shown.
|
|
@ -15,20 +15,51 @@ Dependencies: ???
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <list>
|
|
||||||
#include <set>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// function prototypes
|
// function prototypes
|
||||||
|
// NONE
|
||||||
|
|
||||||
// class definitions
|
// class definitions
|
||||||
|
// NONE
|
||||||
|
|
||||||
// main program
|
// main program
|
||||||
int main(int argc, char **argv)
|
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
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue