How to take user inputs with spaces in C:
In this post, we will learn how to take user inputs with blank spaces in C. I will show you different approaches with drawbacks.
Problem with reading string inputs with scanf
:
We can use the scanf
function to read a string input. The scanf
function is used to read data from stdin
or standard input stream. The first parameter of the scanf
function is used to define the data to read. For strings, we can provide %s
as the first parameter. For example,
#include<stdio.h>
int main()
{
char input[30];
printf("Enter a string: ");
scanf("%s", input);
printf("You have entered: %s\n", input);
return 0;
}
This example asks the user to enter a string. It uses the scanf
function to read that value and assigns it to the character array input
. The second printf
function is printing that value.
If you run the above program, it will work as below:
Enter a string: hello
You have entered: hello
Enter a string: hello world
You have entered: hello
i.e. it reads the character until a blank space is found.
Approach 1: How to use scanf
with a different format specifier:
We can use the scanf
function with a different format specifier to read strings with spaces. If we pass the format specifier %[^\n]
as the first parameter of scanf
, it will read all characters until a newline character, \n
is found. We can use this format specifier to read a string with blank spaces in it.
Let me change the above program to use this format specifier:
#include<stdio.h>
int main()
{
char input[30];
printf("Enter a string: ");
scanf("%[^\n]", input);
printf("You have entered: %s\n", input);
return 0;
}
- Download it on GitHub
If you run this code, it will read a string with blank spaces.
Enter a string: hello world
You have entered: hello world
Drawbacks of this approach:
If the user hits the enter before the scanf
function executes, this will not read that string. For example,
#include<stdio.h>
int main()
{
char input[30], secondInput[30];
printf("Enter a string: ");
scanf("%[^\n]", input);
printf("Enter another string: ");
scanf("%[^\n]", secondInput);
printf("First string: %s\n", input);
printf("Second string: %s\n", secondInput);
return 0;
}
This program is trying to read two strings one after another. But, since the user will hit enter
after the first string is entered, the second scanf
function will read that value and since this is a newline character, it will stop reading any more value. So, if you run this program, it will print output as below:
Enter a string: hello world
Enter another string: First string: hello world
Second string:
The second string was not captured.
To avoid it, we can read that intermediate character with a getchar()
function.
#include<stdio.h>
int main()
{
char input[30], secondInput[30];
printf("Enter a string: ");
scanf("%[^\n]", input);
getchar();
printf("Enter another string: ");
scanf("%[^\n]", secondInput);
printf("First string: %s\n", input);
printf("Second string: %s\n", secondInput);
return 0;
}
- Download it on GitHub
It will work as expected:
Enter a string: hello world
Enter another string: hello universe
First string: hello world
Second string: hello universe
Approach 2: Read a string with blank spaces with the fgets()
method:
We can use the fgets()
method to read a string with blank spaces. The syntax of this method is:
char *fgets(char *str, int n, FILE *file)
- The first parameter,
str
is a pointer to the character array. It stores the string in this array. - The second parameter,
n
is the maximum number of characters from the string to read. - The third parameter,
file
is the pointer to theFILE
. We can passstdin
to read the string from console.
Note that fgets
adds a new-line character at the end of the string automatically.
Let’s use fgets
to read strings with blank spaces from console:
#include<stdio.h>
int main()
{
char input[30], secondInput[30];
printf("Enter a string: ");
fgets(input, 30, stdin);
printf("Enter another string: ");
fgets(secondInput, 30, stdin);
printf("First string: %s", input);
printf("Second string: %s", secondInput);
return 0;
}
- Download it on GitHub
It will give output as below:
Enter a string: hello world
Enter another string: hello universe
First string: hello world
Second string: hello universe
We removed the newline characters \n
from the last two printf
statements as fgets
adds a newline character to the end of input
and secondInput
.
You might also like:
- C program to find the distance between two cities in different units
- C program to subtract two user input numbers
- C program to print the current date-time in different formats
- C program to find the perimeter of a rectangle
- C program to check if a character is an Alphabet or not
- C program to print an alphabet triangle with user-input height