Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Aug 19th, 2005, 9:46 PM   #1
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
How to determine if it's a word

Hi,

I'm working on this problem on how to count the average number of letters per word for a given input. The input is ended with a EOF in stead of the <Enter> key.

The following is my current code.
#include <stdio.h>
#include <ctype.h>

int main(void)
        {
        unsigned int letters = 0, words = 0;
        int ch;

        printf("Enter something: ");
        while((ch = getchar()) != EOF)
                {
                words += !!isspace(ch);
                letters += (!!isupper(ch) || !!islower(ch));
                }
        printf("\nNumber of words: %u", words);
        printf("\nThe average letters per word is %.2f\n", (float)letters / words);

        return 0;
        }

The problem is that my logic of determing a word has having a space following it is not correct as multiple spaces will cause the word count to be off. Do you guys have any algorithms for doing a word count?
aznluvsmc is offline   Reply With Quote
Old Aug 19th, 2005, 10:08 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Finding a letter will turn a gate to True.

Finding a space will turn the gate to False.

Right before the gate is turned False, if it used to be True, then the wordcount += 1.

:p


Since I don't know C, this is it implemented in Python (because I'm bored):

x = """
Try  to  count   the   number of words in   this!


Yo..."""



x = x.replace('\n',' ') + ' ' #Python registers newline as a word

words = 0
gate = False

for a in x:
    if a == ' ':
        gate = True
    else:
        if gate:
            words += 1
        gate = False

print words
>>> 10



Then the average number of letters could be calculated by changing:
gate = True

to:
gate += 1

, and then registering gate to an array before it resets to 0.
Sane is offline   Reply With Quote
Old Aug 19th, 2005, 11:29 PM   #3
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
I'm not really sure what you mean there since I don't know Python.
aznluvsmc is offline   Reply With Quote
Old Aug 20th, 2005, 2:02 AM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Just do what I said: :p


The gate variable starts at 0.

Finding a letter will turn the gate to 1.

Finding a space will turn the gate to 0.

Right before the gate is turned 0 when a space is found, if the gate == 1: then the wordcount += 1.
Sane is offline   Reply With Quote
Old Aug 20th, 2005, 6:14 AM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Why do you use !!isspace(ch)? I take it this is to make sure it only returns 1 or 0, but wouldn't this be better done by using the ternary operator, rather than resorting to some hack?
words += isspace(ch) ? 1 : 0;
Or, as you're not incrementing the variable at all when isspace(ch) returns 0, why bother with that, even?
if (isspace(ch))
{
    words++;
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 20th, 2005, 6:27 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You need to tokenize on more than spaces. I don't think you'd want to assign the '.' to the letter count of "spaces" in the foregoing sentence. How many words is, "I went to the fair.I came back."? How many words is, "He has a Ph.D." Counting unique words is even less trivial. How many different words is, "He is a frog among frogs."?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 20th, 2005, 6:30 AM   #7
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by DaWei
"He is a frog among frogs."?
is his name Jeremiah ?
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Aug 20th, 2005, 6:41 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Dang you, Steven, that'll be running through my head all cottonpickin' day now!
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 20th, 2005, 9:05 AM   #9
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
Quote:
Originally Posted by Ooble
Why do you use !!isspace(ch)?
This was a programming trick I learned in school. I just wanted to employ it so that I don't have to use an if statement. There isn't any real good reason to use a double negation. I just wanted to be fancy. Still learning what I can and can't do with C. :o
aznluvsmc is offline   Reply With Quote
Old Aug 20th, 2005, 10:24 AM   #10
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by DaWei
Dang you, Steven, that'll be running through my head all cottonpickin' day now!
Well, there are certainly worse tunes to have boppin' around up there. (like: "There Coming to Take Me Away" - Napoleon XIV.... colleagues start getting worried when you sing that from within your cubicle )
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs 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




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

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