C print each word in a separate line from an input string

Introduction:

In this C programming tutorial, we will learn how to read one user input string and how to print each word of that string in separate lines.

Our program will ask the user to enter a string. It will read that string and print each word of that string in separate lines.

For example, if the input string is Hello World !!, it will print the below output :

Hello
World
!!

Method 1: C program to print the words in separate lines with a for loop:

The following C program prints the words of a user-input string in separate lines by using a for loop:

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

// 1
#define MAX_SIZE 100

int main()
{
    // 2
    char str[MAX_SIZE];

    // 3
    printf("Enter a string : ");
    fgets(str, MAX_SIZE, stdin);

    int i;
    int end;
    int start = 0;

    // 4
    for (i = 0; i < strlen(str); i++)
    {
        // 5
        if (str[i] == ' ' || i == strlen(str) - 1)
        {
            end = i;
            printf("%.*s\n", (end - start), str + start);
            start = i + 1;
        }
    }

    return 0;
}

Download it on GitHub

Explanation:

The commented numbers in the above program denote the step numbers below:

  1. The variable MAX_SIZE is initialized as 100. It is the maximum size of a string that the program can read.
  2. The variable str is a character array of size MAX_SIZE. We will assign the user input string to this character array.
  3. It asks the user to enter a string. It reads that string and assigns it to the str variable by using the fgets method. We are defining three variables: the variable i is used in the loop, the variable start is used to assign the current start index of the current word, and the variable end is used to assign the current end index of the current word.
  4. It uses one for loop to iterate through the characters of the user input string.
  5. If the current loop iteration reads any blank space or if the value of i is equal to the end index of the string, i.e. if any end of a word is reached, it prints that word starting from the start index to the end index of the string for that word. The variables start and end are used to indicate the start index and end index of the current word.

C program example to print words of a string in different lines

Sample Output:

If you run the above program, it will print outputs as below:

Enter a string : hello world !!
hello
world
!!


Enter a string : the quick brown fox jumps over the dog
the
quick
brown
fox
jumps
over
the
dog

Method 2: Without using the start and end index variables for the words:

We can also print the characters of the string one by one in the loop and print a newline character if any blank character or end of the string is found. The following program shows how it can be done:

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

#define MAX_SIZE 100

int main()
{
    char str[MAX_SIZE];

    printf("Enter a string: ");
    fgets(str, MAX_SIZE, stdin);

    int i;

    for (i = 0; i < strlen(str); i++)
    {
        if (str[i] == ' ' || i == strlen(str) - 1)
        {
            printf("\n");
        }
        else
        {
            printf("%c", str[i]);
        }
    }

    return 0;
}

Download it on GitHub

  • This is similar to the previous program. It reads the string and assigns it to the str variable.
  • In the for loop, it prints the ith index character of the string on each iteration. If any blank space or end index is found, it prints a new line.

It will print similar outputs.

You might also like: