Thread: count words program problem

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    7

    count words program problem

    Hi
    Can one of you experts give me a hint with this. The code takes words typed in and counts them. My problem is that i want to modify it so the words are displayed one per line as in
    this
    is
    some
    text.
    heres the code
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    
    #define true 1
    #define false 0
    #define kmaxlinelength 200
    #define kzerobyte 0
    
    void readline(char line[]);
    int countwords(char line[]);
    
    int main(void)
    {
        char line[kmaxlinelength];
        int numwords;
        
    printf("please type a line of words\n");
    
    readline(line);
    numwords=countwords(line);
    
    printf("\nthis line has %d word", numwords);
    
              if(numwords !=1)
               printf("s");
      
               printf("\n%s\n", line);
      
               getch();
      
               return 0;
    }  
      
    void readline(char line[]) //this function reads the input
      {  
        while((*line=getchar()) != '\n')
        line++;
        
        *line= kzerobyte;
      }
    
    int countwords(char line[])//this function counts words
    {
        int numwords, inword;
        
            numwords= 0;
            inword= false;
            
        while(*line != kzerobyte)
        {
            if(! isspace(*line))
            {
                if(!inword)
                {
                   numwords++;
                    inword=true; 
                }
              }    
                else
                    inword=false;
                    /*i thought about entering a carriage
                    return here though i don't know how exactly*/
                    line++;
                }
                return numwords;
            }
    any help would be appreciated

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Hi,
    I you could incorporate it into your "countwords function", by using another string to hold the characters as you check them and print them out to a new line when you find the end of a word.
    This wouldn't take too much extra code really

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Wow your style unorthodox but it seems to work , why would you would assign a char * for getchar().
    Code:
    void readline(char line[]) //this function reads the input
      {  
        while((*line=getchar()) != '\n')
        line++;
        
        *line= kzerobyte;
      }
    getchar() prototype is
    Code:
     int getchar(void)
    meaning it returns an int which is the character from stdin.
    Also
    Code:
    *line= kzerobyte;
    if your trying to attach the null character then attach the null character that is
    Code:
    '\0'
    as it stands you have
    Code:
    #define kzerobyte 0
    I'm not sure why your code works, but perhaps you were trying to use gets whose prototype is
    Code:
    char *gets(char *)
    not that its the safest choice.

    As it stands you store all your words into a charater array and then expect this to work
    Code:
     printf("\n%s\n", line);
    this will only print out what you have stored in the array and add a newline when its done. If you inist on coding the way you did, you'll have to check for spaces from stdin and put a '\n' whenever there is a space.


    Also your use of the <conio.h> is not needed, as it is not portable. You can repalce
    Code:
    getch
    with
    Code:
     getchar()
    .

    adding to your style I guess nothing is wrong wit it:
    Code:
    if(numwords !=1)
               printf("s");
    why not
    Code:
     printf("\nthis line has %d %s", numwords, (numwords !=1 ? "words" : "word") );
    my two cents

    [edit] I guess technically since a char is represented by an integar, a ptr to a char is basically still an int. unorthodox though. interested to see what the other members say[/edit]
    Last edited by caroundw5h; 10-18-2004 at 04:10 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    getchar will return a value in the valid range of characters for type char, or EOF. So the only real problem with them doing it this way is if it happens to return EOF, and they assign it to a char which ends up changing it to some other value due to it not being in the valid range for a char.

    Also, null and the decimal 0 evaluate to the same thing.

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

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks for the reply. you learn something new everyday.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Your best bet might be to use strtok() to separate the string into individual words, and then print them out. I have a nice little version of your program that does just that...


    Just remember that strtok() modifies the string it is working on, so if you don't want the string changed, you will want to make a copy and then use strtok() on the copy.
    Last edited by kermit; 10-18-2004 at 08:00 PM.

  7. #7
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    This should work nicely for your purposes. It simply prints each char and replaces spaces with newlines.


    PHP Code:
    void printWords( const char *line )
    {
        for ( ; *
    line != '\0' line++ )
        {
            
    printf "%c" , *line == ' ' '\n' : *line );
        }
        
    printf "\n" );

    Last edited by Scribbler; 10-18-2004 at 11:07 PM.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    That is a nicer solution than the one I was thinking of. I would change one little thing, since the OP is #including ctype.h anyway...

    Code:
    Iprintf ( "%c" , isspace(*line)? '\n' : *line );
    Thyat catches tabs and stuff too..

    ~/

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    7
    thanks for the input guys. I'll have a go at some of your suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 5
    Last Post: 09-28-2004, 12:38 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. word count program need a bit of help!
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-19-2002, 08:15 PM