|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
counting words
I have a new project which I would really appreciate your help with. Among the tasks of this project, I need to count the words of a text file. I don't understand why the code to count the characters works. If I did, perhaps I could figure out how to write the code that would count all the {groups of letters between the spaces} also known as {words}. Can anyone explain or help?
the piece of code I'm having trouble with is: while (getline(InFile, S)) { TotalChars+=S.length(); TotalLines++; TotalWords++; AverageWordLength=((TotalWords+TotalChars)/2); } the particular line that is incorrect is TotalWords++; which of course at present is returning the same value as TotalLines. Thanks! |
|
#2
|
||||
|
||||
|
I'd probably throw the file into a string, split the string on spaces, and return the size of the array. Failing that, you could check to see if the current character is a space, and if it is, increment TotalWords. That seems rather less efficient, though.
|
|
#3
|
|||
|
|||
|
I have done so, but I guess I don't know the code to use to split the string on spaces and return the size of the array. Want to see the full "program"? It's only 51 lines.
|
|
#4
|
||||
|
||||
|
Here's an example of splitting a string using C++
Code:
void CTokenEx::Split(CString Source, CString Deliminator, CStringArray& AddIt)
{
CString newCString;
CString tmpCString;
CString AddCString;
int pos1 = 0;
int pos = 0;
newCString = Source;
do {
pos1 = 0;
pos = newCString.Find(Deliminator, pos1);
if ( pos != -1 ) {
CString AddCString = newCString.Left(pos);
if (!AddCString.IsEmpty())
AddIt.Add(AddCString);
tmpCString = newCString.Mid(pos + Deliminator.GetLength());
newCString = tmpCString;
}
} while ( pos != -1 );
if (!newCString.IsEmpty())
AddIt.Add(newCString);
}
You can call it like this: Split(originalString, " ", arrayOfWords); That middle parameter is the delimeter From what I understand VisualC++ has a Split function built in... I haven't used it though. |
|
#5
|
|||
|
|||
|
I'm being asked to write a program that displays the number of words and the average word length in a text file. I've managed to write a program that displays this information when the user inputs a sentence, but not when the program is reading the text file.
|
|
#6
|
||||
|
||||
|
Are you getting an error?
Or do you not know how to read files? Refer to http://www.cplusplus.com/doc/tutorial/tut6-1.html for help with files |
|
#7
|
|||
|
|||
|
Thanks that was a good tutorial...
I am just posting today to let you know what code I ended up submitting.... Perhaps posting solutions now and then will help the newcomer... I didn't come up with how to find the average word length with it so I'll probably only get 50% credit if that. Code: /* Count Words program Pre: InFile contains a a string. Post: returns number of words and average word length of InFile */ #include <string> #include <fstream> #include <iostream> using namespace std; main() { string word; ifstream InFile; int i = 0; InFile.open("source.txt"); while(InFile >> word) { i++; } cout <<"Total Words: "<< i << '\n'; return 0; } Thanks for your help everybody! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > counting words |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|