Thread: Problem with letter and word counting

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

    Angry Problem with letter and word counting

    I have written the following code to tell the user how many regular letters and how many words are in each sentence they type. However, the program only counts the first word and the letters of that word. Anybody know how to extend it beyond this so that it will count all input entered by the user?

    I know the answer is probably staring me in the face but i just can't see it. Thanx.

    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);
    	next_word++;
    	chr = getchar();
    	putchar(chr);
    	
    }
    if (chr = '.')
    {
    letter_count = (letter_count - 1);
    printf ("%10d characters long\n", letter_count);
    
    printf("Words : %d\n", word_count);
    	printf("Letters : %d\n", letter_count);
    return 0;
    }
    else if (chr = EOF)
    {
    printf ("%10d characters long", letter_count);
    return 0;
    }
    else if (chr = isspace(chr))
    {
    return 1;
    }
    return 1;
    
    }
    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    Ok, done and done. Any other tips?

    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);
    	next_word++;
    	chr = getchar();
    	putchar(chr);
    	
    }
    	if (chr == '.')
    	{
    		letter_count = (letter_count - 1);
    		printf ("%10d characters long\n", letter_count);
    
    		printf("Words : %d\n", word_count);
    		printf("Letters : %d\n", letter_count);
    		return 0;
    	}
    		else if (chr == EOF)
    	{
    		printf ("%10d characters long", letter_count);
    		return 0;
    	}
    		else if (chr == isspace(chr))
    		{
    		return 1;
    		}
    		return 1;
    
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The !isspace(chr) breaks the while loop on the first space character.
    Originally posted by wordup
    Any other tips?
    Think about using isalpha() or isalnum() to see whether you are in a word; maybe set a boolean that indicates whether you are currently in a word or not. Investigate ispunct() as a possible replacement for chr == '.'.

    Why repeat your loop conditions in the if tree?

    You may want to make variables chr, word_count, and letter_count local to read_and_count() since that is the only place they are used.


    Mostly nitpicks:
    In main() I'd follow the prompt with fflush(stdout) to ensure that it is printed. I'd also use puts() instead of printf() when no formatting is required.

    I prefer to use the increment and decrement operators (++letter_count and --letter_count instead of letter_count = (letter_count +1) and letter_count = (letter_count -1)).

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Any other tips?
    To add to whats already been said: Yes, your indentation is still wrong. The following is your code tidied up (no changes, just the layout)
    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);
            next_word++;
            chr = getchar();
            putchar(chr);
        }
    
        if (chr == '.')
        {
            letter_count = (letter_count - 1);
            printf("%10d characters long\n", letter_count);
    
            printf("Words : %d\n", word_count);
            printf("Letters : %d\n", letter_count);
            return(0);
        }
        else if (chr == EOF)
        {
            printf("%10d characters long", letter_count);
            return(0);
        }
        else if (chr == isspace(chr))
        {
            return(1);
        }
    
        return(1);
    }
    >>while (chr != EOF && !isspace(chr) && chr != '.')
    The first time you go through this loop, you haven't actually read anything into chr. Personally, I think I'd have controlled the loop like this:
    Code:
    while ((c = getchar()) != EOF)
    {
        /* count words and letters here */
    }
    Either that, or use fgets() to grab a buffer load of data first. Either way, it's up to you to do what you're comfortable with.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 06:13 PM
  2. identifying first letter of word
    By speedyboy in forum C Programming
    Replies: 11
    Last Post: 03-19-2006, 07:56 AM
  3. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 05:38 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Capatalizing the first letter of a word
    By cprog in forum C Programming
    Replies: 0
    Last Post: 12-07-2002, 06:58 PM