![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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![]() 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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
I'm not really sure what you mean there since I don't know Python.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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; if (isspace(ch))
{
words++;
} |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
![]()
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Quote:
Still learning what I can and can't do with C. :o |
|
|
|
|
|
|
#10 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
)
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|