General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 2nd, 2003, 05:30 PM
iamkim iamkim is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 iamkim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #2  
Old December 3rd, 2003, 07:37 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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.

Reply With Quote
  #3  
Old December 3rd, 2003, 07:44 AM
iamkim iamkim is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 iamkim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old December 3rd, 2003, 08:26 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
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.

Reply With Quote
  #5  
Old December 3rd, 2003, 09:54 AM
iamkim iamkim is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 iamkim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #6  
Old December 3rd, 2003, 03:52 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
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

Reply With Quote
  #7  
Old December 8th, 2003, 03:52 PM
iamkim iamkim is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 iamkim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > counting words


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT