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:
- The variable
MAX_SIZE
is initialized as100
. It is the maximum size of a string that the program can read. - The variable
str
is a character array of sizeMAX_SIZE
. We will assign the user input string to this character array. - It asks the user to enter a string. It reads that string and assigns it to the
str
variable by using thefgets
method. We are defining three variables: the variablei
is used in the loop, the variablestart
is used to assign the current start index of the current word, and the variableend
is used to assign the current end index of the current word. - It uses one
for
loop to iterate through the characters of the user input string. - 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 thestart
index to theend
index of the string for that word. The variablesstart
andend
are used to indicate the start index and end index of the current word.
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 thestr
variable. - In the
for
loop, it prints thei
th 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:
- C program to read the contents of a file character by character
- How to use strchr to print substring starting from a specific character
- fabs function in C with example
- C program to check if a number is palindrome or not
- C program to find compound interest using user input values
- C program to find out the palindrome number in a range
- C program to count and print frequency of each letter in a word