#include <iostream>
#include <string>
usingnamespace std;
// function prototype
int countWords(char*);
int countWords(string);
int main()
{
char* newPtr = nullptr;
constint SIZE = 101;
char usrInput[SIZE];
newPtr = usrInput;
cout << " Word Counter" << endl;
fstream "Gettysburg.txt", ios::";out | ios::";binary);
fstream "output.txt", ios::";out | ios::";binary);
cin.getline(newPtr, SIZE);
//getline(cin, sent);
//display the number of words contained file and output them to a textfile
cout << " Correct total of words counted in Gettysburg file are " << countWords(newPtr) << endl;
cout << " check the output file " << countWords(newPtr) << " words" << endl;
fstream "output.txt", ios::";out | ios::";binary);
return 0;
}
//======================================================================
// function definition - counts how many words are in the string that is passed as an arg
int countWords(char* sentence)
{
int totalWords = 0; // counts words
if (*(sentence) != '\0') // if some input then there is at least 1 word
++totalWords;
for (int count = 0; *(sentence+count) != '\0'; count++)
{
if (*(sentence + count) == ' ')
{
++totalWords;
}
}
return totalWords;
}
There are many ways to do this but here is one using the strtok() function and by a careful selection of characters in the the separators[] array as shown.
Note there are a few discrepancies due to the 100 character line limit and just cutting and pasting the source file. Each line in the original needs to be properly terminated or the 100 is substantially increased.
Four score and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal Now we are engaged in a great civil war testing whether that nation or any nation so conceived and dedicated can long endure We are met on a great battlefield of that war We have come to dedicate a portion of that fie ld as a final resting place for those who here gave their lives that that nation might live It is altogether fitting and proper that we should do this But in a larger sense we can not dedicate we can not consecrate we can not hallow this ground The brave men living and dead who stru ggled here have consecrated it far above our poor power to add or detract The world will little n ote nor long remember what we say here but it can never forget what they did here It is for us th e living rather to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced It is rather for us to be here dedicated to the great task remaining before u s that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion that we here highly resolve that these dead shall not have died in vain that this nation under God shall have a new birth of freedom and that government of t he people by the people for the people shall not perish from the earth
+++ ENDS +++
No. of words: 276
Program ended with exit code: 0
As the first post is C++, this can be easily done in C++. If you extract a string from a stream, it extracts up-to a white space char. Assuming words are separated by white-space, then just counting the number of words extracted will give the number of words. Consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream ifs("gettysburg.txt");
if (!ifs.is_open())
return (std::cout << "File cannot be opened\n"), 1;
std::ofstream ofs("output.txt");
int cnt {};
for (std::string word; ifs >> word; ++cnt);
std::cout << cnt << " words\n";
ofs << "Correct total of words counted in Gettysburg file are " << cnt << '\n';
}
Giving:
276 words
c:\MyProgs>type output.txt
Correct total of words counted in Gettysburg file are 276