Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner: Counting Words & Alphabetic numbers

Status
Not open for further replies.

Ireland1978

Programmer
Sep 29, 2005
17
GB
Hi all,

How would I go about writing code which would count the amount of alphabetic characters entered, and also the amount of words?
I know it would involve some sort of loop.

eg. Input: How now brown cow
Output:
Words = 4
Letters = 14

I would ideally like to be just pointed in the right direction, as I like working these out for myself, but don't know where to start!!

I presume I'd use the spaces to determine the amount of words - would I use the ASCII notation for space?
Thanks
 
int i=0;
while(input!='\0')
{
if(isalpha(input))
alphabets++;
if(input == ' ')
word++;
}
 
That works for total letters, but the words bit will need some tweaking surely ... ? After all, suppose my string input was simply " " ?? ;-)

I won't give full code details since Ireland said he likes to work stuff out, but I guess a good way would be to have two records for the number of alphabetic characters: alphabets, and alphabets_current_word. The latter gets reset to zero each time you encounter a space. Then you can use that to help define when you start a new word.

I can give a more complete description if need be, including code examples, but I hope that will suffice. :)

-- Joe
 
What about other "whitespace" characters? Carriage return, line feed, and tabs can also terminate a word.
 
A... what? Do you mean those funny other people with the bumps on their chests? I thought they lived outside and only came in to feed or when it rained. And that their brains were all screwy so they couldn't work computers unless it was to set up a webcam and dance in front of it...

I'll get me coat. ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top