![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2006
Posts: 1
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,825
Rep Power: 5
![]() |
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! |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
Also this test
else if (*string == ('a' <= *string && *string <= 'z') || ('A' <= *string && *string <= 'Z'))Also don't forget to initialise gate |
|
|
|
|
|
#4 |
|
Professional Programmer
|
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') |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Nov 2006
Posts: 31
Rep Power: 0
![]() |
I have something that might help, I will PM you if I find it.
__________________
www.narfco.com |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |