Thread: Word Count

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    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.





    Output Specification:

    Sample Run:



    Type the sentence: What a lovely day.



    Output:



    What 5 characters.

    a 1 character.

    lovely 6 characters.

    day 3 characters.



    The longest word in the sentence is 6 characters long

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Show us what you've got, and what you're stuck on. No pain, no gain. Read the FAQ. Use code tags.
    [/broken record]

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Once again quzah...tact is good.....


    simple -

    Read the Announcements...particuarlly the second one reguarding homework. People arn't going to do all of your work for you. Show us what you've got. Where's it going wrong? What is it doing?

    People don't like doing all of your work for you. Show us what you've got. It is your homework after all. People are willing to help you, but they don't want to do your work.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    This is what i have so far.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int read_and_count(void);
    
    int word_count = 0;
    int letter_count = 0;
    int next_word = 0;
    
    int main(void)
    {
    
    	while(read_and_count == 1)
    	while(next_word == 2)
    	
    }
    int read_and_count(void)
    {
    while (chr != EOF; chr != isspace(chr); chr != ".") 
    {
    	getchar(chr);
    	;
    	printf("%c", chr);
    	letter_count = letter_count +1;
    }
    return 2;
    
    int next_word(void)
    {
    printf ("%10d characters long\n", letter_count);
    letter_count = 0;
    ;
    }
    If (chr != EOF; char != isspace(chr); chr != ".")
    {
    return 0;
    }
    else
    {
    return 1;
    }
    Code Tags added by kermi3

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    And now for the second thing quzah was talking about...code tags:

    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.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Thanks for the help with the code tag, hopefully i will get some help with this now.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main(void)
    {
    
    	while(read_and_count == 1)
    	while(next_word == 2)
    	
    }
    A few major problems here:

    1) You have a nested loop which doesn't do anything and is a non-terminated statement.

    2) You are incorectly using functions. To call a function, do the following:

    a) To call a function by itself:

    myfunction( );

    b) To calll a function and assign or use the return value:

    x = myfunction( );

    /* or */

    if ( myfunction( ) == x ) /* etc etc */

    3) the function 'main' always returns a value. So:

    Code:
    int main( void )
    {
        /* code here */
    
        return 0;
    }
    We'll start there.


    [edit]
    Gah. I looked at the rest, and decided you need quite a bit more help. Here we go...


    Code:
    int read_and_count(void)
    {
    while (chr != EOF; chr != isspace(chr); chr != ".") 
    {
    	getchar(chr);
    	;
    	printf("%c", chr);
    	letter_count = letter_count +1;
    }
    return 2;
    1) You use a variable 'chr', but you never define the variable anywhere. You need to start like this:

    int read_and_count( void )
    {
    int chr;

    2) Any time you are testing for EOF, you need your variable to be an integer, and not a char. That is why I used an integer.

    3) A semicolon on a line by itself is what we call an empty expression. It has on effect. It's main use is when you do something like:

    while( (c=fgetc(stdin)) != '\n' ) ;

    The semicolon at the end of this while loop is being used instead of a normal code block to basicly make the loop only process it's arguments and do nothing else.

    So, in your example:
    Code:
    	getchar(chr);
    	;
    	printf("%c", chr);
    The second line doesn't do anything at all. As such, there is no need for it.

    Code:
    }
    return 2;
    Remember this: For every opening braces or parenthesis, you need a closing one.

    As such, you are missing the closing } which should be after the "return 2;" line.
    [/edit]

    We'll stop there for now (again).

    Quzah.
    Last edited by quzah; 10-04-2002 at 09:09 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    1. chr is not defined.

    2. While loop expression is set up wrong.

    3. If expression is set up wrong.

    We will help- but do you have your text book or reference to help as well.


    Mr. >C.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    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!
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int read_and_count(void);
    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();
    	
    	
    }
    int read_and_count(void)
    {
    while (chr != EOF; chr != isspace(chr); chr != ".") 
    {
    	getchar(chr);
    	;
    	printf("%c", chr);
    	letter_count = letter_count +1;
    }
    
    If (chr = "."; chr = EOF)
    {
    letter_count = (letter_count - 1);
    printf ("%10d characters long\n", letter_count);
    letter_count = 0;
    }
    else If (chr != EOF; char != isspace(chr); chr != ".")
    {
    return 1;
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
                */
    Take it from there and see what you can do.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    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

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int read_and_count(void);
    int chr;
    int word_count = 0;
    int next_word = 0;
    int letter_count = 0;
    int main(void)
    {
    	printf("Please type a sentince\n");
    	read_and_count(void)
    	return 0;
    }
    int read_and_count()
    {
    while(char != EOF && !isspace(chr) && chr != '.')
    {
    	letter_count = (letter_count +1);
    	chr = getchar();
    	putchar(chr);
    }
    if (chr = '.')
    {
    letter_count = (letter_count - 1);
    printf ("%10d characters long\n", letter_count);
    letter_count = 0;
    return 0;
    }
    else if (chr = EOF)
    {
    printf ("%10d characters long", letter_count);
    return 0;
    }
    else if (chr = isspace(chr))
    {
    return 1;
    }

  12. #12
    Registered User Dcower's Avatar
    Join Date
    Aug 2002
    Posts
    8
    I hope this helps somewhat...

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int read_and_count(void);
    int chr;
    int word_count = 0;
    int next_word = 0;
    int letter_count = 0;
    int main(void)
    {
    	printf("Please type a sentence\n");
    	read_and_count();
    	return 0;
    }
    int read_and_count(void)
    {
    while(chr != EOF && !isspace(chr) && chr != '.')
    {
    	letter_count = (letter_count +1);
    	chr = getchar();
    	putchar(chr);
    }
    if (chr = '.')
    {
    letter_count = (letter_count - 1);
    printf ("%10d characters long\n", letter_count);
    letter_count = 0;
    return 0;
    }
    else if (chr = EOF)
    {
    printf ("%10d characters long", letter_count);
    return 0;
    }
    else if (chr = isspace(chr))
    {
    return 1;
    }
    return 1;
    }
    If you want to know what I changed just ask...


    dillon

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    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!

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  2. word count
    By unity_1985 in forum C Programming
    Replies: 3
    Last Post: 07-29-2007, 10:34 AM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. word count troubles
    By Hoser83 in forum C Programming
    Replies: 13
    Last Post: 02-09-2006, 09:12 AM
  5. Replies: 5
    Last Post: 09-28-2004, 12:38 PM