C program to using gets() & fgets() function to read complete string with spaces
ADVERTISEMENTS
C program to using gets() & fgets() function to read complete string with spaces.
There are you will learn how to read a complete string with spaces by using the gets() & fgets() function in the C language.
1. By using the gets() function
// C program to using gets() function to read complete string with spaces
#include <stdio.h>
#define MAX 1024
// gets() function will be read maximum 1024 characters
int main() {
char p[MAX];
printf("Enter a string::\n");
gets(p);
// Output
printf("\nThe input string is:\n%s\n", p);
return 0;
}
Output:
Enter a string::
hello program
The input string is:
hello program
Warning: the `gets' function is dangerous and should not be used.
2. By using fgets() function
// C program to using fgets() function to read complete string with spaces
#include <stdio.h>
#define MAX 1024
// fgets() function will be read maximum 1024 characters
int main() {
char p[MAX];
printf("Enter a string::\n");
fgets(p, MAX, stdin);
// Output
printf("\nThe input string is:\n%s\n", p);
return 0;
}
Output:
Enter a string::
hello program
The input string is:
hello program