Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 29th, 2006, 6:16 PM   #1
boxcar182
Newbie
 
Join Date: Nov 2006
Posts: 1
Rep Power: 0 boxcar182 is on a distinguished road
Trying to write a program to count words

Hello everybody. My first post on this forum and i'm in desperate need of your help.

I'm trying to create a program that will count the number of words in a string. Things that are considered part of a word are any letter or punctuation.

My code right now is: (With TRUE = 1, FALSE = 0)
int count_words(const char *string)
{
  int i = 0;  /*counter for loop*/
  int length = mystrlen(string); /*determine the length of the string */
  int words = 0; /*gives the total number of words in the string */
  int gate;
 
  
  for(; i < length; i++, string++)
  {
   if(*string == ' ' || '\n' || '\t')
   {
     gate = FALSE;
   }
   else if (*string == ('a' <= *string && *string <= 'z') || ('A' <= *string && *string <= 'Z'))
   {
     gate = TRUE;
   }
   if(--gate == TRUE && gate == FALSE)
   {
     words++;
   } 
  }  
  return words;

I was trying to count spaces but the problem that i'm getting is that if i do count spaces for one example:
|This is a simple string.|

my word count is 4 which makes sense. If i were to add 2 it wouldn't be correct. Also i have to check for tabs and newlines, and also there are going to be multiple spaces and tabs and other junk.

What i was trying to do was have two things, TRUE or FALSE. When the value was correct (i.e. when the string was a-z or A-Z) the result would be TRUE. When it's a space, tab, or newline it would be FALSE.

Now the problem i think i'm having is that i'm trying to figure out how to write the code to determine if the previous result was TRUE and it is now FALSE add 1 to word count.

If anyone knows what i'm doing wrong please respond. Thanks in advance!
boxcar182 is offline   Reply With Quote
Old Nov 29th, 2006, 9:28 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,839
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
If you want to know what the previous result was, make a "lastGate" variable that is assigned the value of "gate" at the very end of the for loop.

That way, you can check for multiple spaces, as you mentioned earlier, with:

if (lastGate == TRUE && gate == FALSE)

Don't forget to initiate "lastGate" with a default value!
Sane is offline   Reply With Quote
Old Nov 29th, 2006, 9:51 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
Also this test
   else if (*string == ('a' <= *string && *string <= 'z') || ('A' <= *string && *string <= 'Z'))
The bit in red makes no sense - is it left over from an earlier version. You are comparing char value with a boolean expression.

Also don't forget to initialise gate
The Dark is offline   Reply With Quote
Old Nov 29th, 2006, 10:09 PM   #4
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
There's 2 things wrong with this bit:

if(*string == ' ' || '\n' || '\t')

First is that it should be *(string+i).

Second, you can't line up the ||'s like that. You need something like:

if(*(string+i) == ' ' || *(string+i) == '\n' || *(string+i) == '\t')
andro is offline   Reply With Quote
Old Nov 30th, 2006, 12:31 AM   #5
Narfco
Programmer
 
Join Date: Nov 2006
Posts: 31
Rep Power: 0 Narfco is on a distinguished road
I have something that might help, I will PM you if I find it.
__________________
www.narfco.com
Narfco is offline   Reply With Quote
Old Nov 30th, 2006, 2:55 AM   #6
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
strtok?

i don't know, just poking in the dark...
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Language display in program Prm753 C++ 3 May 30th, 2006 5:45 PM
PigLattin Converter, count number of words used in dictionary. MrSmiley Python 2 Oct 17th, 2005 4:47 PM
crack these questions if u can!!! shagan C++ 18 Apr 3rd, 2005 6:47 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM
Help: Program Count Words Nano Java 6 Jan 30th, 2005 9:40 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC