In C, we may need to read the input entered by the user that includes the paragraph of text with spaces. In this article, we will learn how to read a paragraph of text with spaces in C.
Reading a Paragraph with Space in C
In C, reading a paragraph of text that includes spaces can be done by using fgets function, This function reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.
C Program to Read a Paragraph of Text with Spaces
The below program demonstrates how we can use the fgets function to read a paragraph of text with Spaces:
// C program to demonstrate fgets function to read a
// paragraph of text
#include <stdio.h>
int main()
{
// Prompt user to enter paragraph of text
printf("Enter a paragraph of text:\n");
// Declare a char array to store the input
char paragraph[1000];
// Read input until newline is encountered
fgets(paragraph, sizeof(paragraph), stdin);
// Display the entered paragraph
printf("Paragraph you entered:\n%s", paragraph);
return 0;
}
Output
Enter a paragraph of text:
Hello Geeks
This is a tutorial on Reading a Paragraph of Text with Spaces in C
Paragraph you entered:
Hello Geeks
This is a tutorial on Reading a Paragraph of Text with Spaces in C
Time Complexity: O(N), where N is the total number of characters in the paragraph.
Auxiliary Space: O(N)
Note: We can also use loops to read multiple paragraphs or continue reading until a specific condition is met.