-5

I want to make a program in C that reads a line of characters and then print each word in the line on separate lines.

This is what i have:

char C;

printf("Write some characters: ");
scanf_s("%c",&C);
printf("%c",C);

As you can see I havent started with the thing I want to do because i dont know if I should use an if-statment or a for-statment.

Robin M
  • 41
  • 1
  • 8
  • 2
    You need to start working through a decent C book for beginners, here's [a good list to start with](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Crowman Sep 08 '13 at 17:02
  • Given the description, I'd probably use a `while` loop. – Jerry Coffin Sep 08 '13 at 17:02
  • 1) Scan a single character and print it, 2) scan the whole line, character by character, and print them, 3) print a line feed (`\n`) now and then. – Beta Sep 08 '13 at 17:03

3 Answers3

2

First, you need to read entire line of the characters, and you're reading only one character:

#include <stdio.h>
#include <string.h>


int main()
{
    int k;
    char line[1024];
    char *p = line; // p points to the beginning of the line

    // Read the line!
    if (fgets(line, sizeof(line), stdin)) {
      // We have a line here, now we will iterate, and we
      // will print word by word:
        while(1){
            char word[256] = {0};
            int i = 0;
            // we are always using new word buffer,
            // but we don't reset p pointer!

            // We will copy character by character from line
            // until we get to the space character (or end of the line,
            // or end of the string).
            while(*p != ' ' && *p != '\0' &&  *p != '\n')
            {
              // check if the word is larger than our word buffer - don't allow
              // overflows! -1 is because we start indexing from 0, and we need
              // last element to place '\0' character! 
              if(i == sizeof(word) - 1)
                 break;

              word[i++] = *p;
              p++;
            }
            // Close the string
            word[i] = '\0';

            // Check for the end of the original string
            if(*p == '\0')
                break;

            // Move p to the next word
            p++;

            // Print it out:
            printf("%s\n", word);
        }
    }

    return 0;
}

I am letting you to try to fix the problem if you have multiple spaces together in the line - it is not that hard once you understand how this is done.

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
0

I see. Now i have done my own solution here and I think it easier to understand:

#include <stdio.h>

void main()
{
char c;

c = getchar();
while(c !='\n')
{
    if (c == ' ')
    {
        printf("\n");
    }
    else
    {
        putchar(c);
    }
    c = getchar();
}
printf("\n");
}
Robin M
  • 41
  • 1
  • 8
0

Read the characters in an array,make a for loop,print them with a endl statement and then run to the closest bookstore and grab a programming book.

S3cube
  • 38
  • 6