Word Count

This is a discussion on Word Count within the C Programming forums, part of the General Programming Boards category; These are the instructions for my c program. I keep getting started then it never seems to work please help! ...

Word Count

These are the instructions for my c program. I keep getting started then it never seems to work please help! Instructions below. Thanks in advance.

Write a C program that reads a sentence (i.e. unknown number of characters ending with a period '.') and prints each word in the sentence on a separate line. The program should also print the length of each word. At the end, it will print the number of characters in the longest word. The sentence must start with a non-blank character and each word must be separated with exactly one blank character. (For this program a word is a sequence of any non-blank characters). There shouldn’t be any spaces between the last word and the period that ends the sentence. Your program should read the sentence one character at a time.

Your program must include a function called read_and_count(), that reads a single word, prints the word and the number of characters, and returns the number of characters. (Hint: The function should continue reading and printing characters one by one until it reads a blank (‘ ‘) or a period (‘.’). Then it should print and return the count to the main function. You must think of a way of how to inform the main function when the last word is read and printed). Your main program should call the function read_and_count() properly so that it can use the value that the function returns to find the number of characters in the longest word.

I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you

For example:

Without code tags:

for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}

With Code Tags:

Code:

for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}

This is of course a basic example...more complicated code is even easier to read with code tags than without.

I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.

Originally posted by simple Ok i went ahead and made some of the changes to the code...i think its getting closer. thanks again for all the help. Hopefully i set up the code tags right this time!

Yep, code tags are done correctly.
I will add comments to your code as I correct it.

Code:

#include <stdio.h>
#include <ctype.h>
int read_and_count(void);
/*
For your first examples globals are
fine, but generally you want to avoid
global variables in most situations.
It makes your code easier to read
if the variables a function uses are
actually in that function.
*/
int chr;
int word_count = 0;
int letter_count = 0;
int next_word = 0;
int main(void)
{
printf("Please type a sentince\n");
read_and_count();
/*
You don't do anything with the
return value of your function here.
You probably want to do
something with it, like store it in
a variable so you can print it
later. Otherwise, if you don't ever
plan on using the return value,
you may as well use a 'void'
for your function instead of making
it return an 'int'.
Again, you need to add a line so
your main function returns a
value. This should do:
*/
return 0;
}
int read_and_count(void)
{
while (chr != EOF; chr != isspace(chr); chr != ".")
/*
Your while( ) loop is wrong.
If you want to test for all of
these values, you will need to
use the following:
while( char != EOF && !isspace(chr) && chr != '.' )
Here, we use the "&&" expression
to test for more than one
expression. Not the ; as you
had.
Additionally, when testing for
a single character value, you
use single quotes instead of
double quotes: ' not "
*/
{
getchar(chr);
/*
This next line does nothing.
Remove it.
*/
;
printf("%c", chr);
letter_count = letter_count +1;
}
/*
'If' should be 'if'. Do not use
a capital I.
For testing truth values, you
use the == and not =. The =
is used to assign a value to a
variable. == is used to check
for equality.
*/

ok i just went threw most of the coding and did a lot of revising. it seem that when i run it on my compilor i am still recieveing a few (hopefully) small error problems. Could you please help me pick out a few more of the problems

Thanks everybody, and thanks dillon for clearing up my last bits of coding it now appears to be working. Im going to make a few more small changes so it runs a little more the way i want it to. But thanks a lot and im done bugging you guys with my questions tonight. You have all be A LOT OF HELP!