Thread: Hello there...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    3

    Hello there...

    I was just browsing for a few days on this board. Nice informations about programming are shared here. I've just encountered a problem. I saw this problem on a user assignement. The problem is something like this : Write a program that lets the user write a sentence. The program should then write each word in the sentence on a new row. You may not use any library functions other than
    scanf and printf to solve this assignment.

    I am really struggling to find the best way of doing this but I can't sort things out ... Does somebody have an idea on how I can sort this out?

    Excuse my english, I'm not an netive english speaker. Regards

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    If you use scanf with the format "%s" then it will read one word at a time. So you can output the result like this in a loop

    Code:
    printf("%s\n", word);

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    3
    Yes but ... how do I know how many words the user enters...

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Each time you print a word, that is one word. So you could keep a counter and then at the end of the input, output the total count.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Location
    Silicon Valley, CA
    Posts
    7
    Here's the word counting program. This is the one in K&R book. It can certainly be the best book for C beginners. I recommend that book to you.

    Code:
    #include <stdio.h>
    
    #define IN 1 
    #define OUT 0
    
    main()
    {
         in c, nl, nw, nc, state;
    
         state = OUT;
         nl = nw = nc = 0;
         while ((c = getchar()) != EOF) {
               ++nc;
               if ( c == '\n')
                    ++nl;
               if ( c == ' ' || c == '\n' || c == '\t')
                  state = OUT;
               else if (state == OUT) {
                      state = IN;
                      ++nw;
               }
         }
         printf("%d %d %d\n", nl, nw, nc);
    
    }
    I am supposing you know all of these grammars and syntax.
    This is a very basic word counting program.
    nw is a value of words. So, the value printed in the middle should show how many words you typed.

    And you should know about EOF signals.

    Mostly, when it comes to "counting" something, it usually comes with an increment operator. ++variable <--- like this.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    3
    Thank you very much for your answer but the problem here is that you are not allowed to use any other functions that the standard printf and scanf functions... This is the trick Regards

    And I still don't get it even if c99tutorials tried to explain it !

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Just try with a simple program first

    Code:
    char word[MAX];
    scanf("%s", word);
    printf("%s\n", word);
    This will read and print one word on a line. You have only to put it into a loop to make it continue to read all words.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by c99tutorial View Post
    Just try with a simple program first

    Code:
    char word[MAX];
    scanf("%s", word);
    printf("%s\n", word);
    This will read and print one word on a line. You have only to put it into a loop to make it continue to read all words.
    But that is NOT his requirement. First, the user has to enter the entire sentence. THEN the program needs to print out each word.

    The logic from the K&R program, could be used. Just delete the getchar() bit, and use the index number, instead.

    Welcome to the forum, and give that a try.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Adak View Post
    First, the user has to enter the entire sentence. THEN the program needs to print out each word.
    You could do scanf and printf in a loop. And use the leading whitespace on the scanf format to skip whitespace " %s". Then it will print each word.

Popular pages Recent additions subscribe to a feed