How to take input in string in C?
Using scanf("%s", str"), we may accept a string as input in C. However, it only takes strings up until the first space is located.
The C program may take a space-delimited string from the user via 4 different techniques.
1) Using gets
The gets () method in C allows you to accept a string input from the user. The gets () method takes a line of text from the keyboard's standard input stream and puts it in the designated character array.
Here is an example code that uses gets () function to take a string input from the user:
#include <stdio.h> int main() { char str[100]; printf("Enter a string: "); gets(str); printf("You entered: %s", str); return 0; }
Output:
The character array str, which has a capacity of 100 characters, is declared in this code. The printf() method is then used to prompt the user for a string. The next step is to read a line of text from the standard input and save it in the str array using the gets() method. Finally, we show the user-entered string using the printf() function.
Due to security concerns, the gets() method has been deprecated in recent versions of the C programming language. It is advised to use the safer fgets() method as an alternative instead.
2) Using fgets
The following syntax can be used to accept a string input in C when using:
fgets(str, size, stdin);
Here, size is the maximum number of characters that may be read, str denotes the character array (string) where the input will be saved, and stdin denotes the standard input stream.
Example:
#include <stdio.h> #define MAX_SIZE 100 int main() { char str[MAX_SIZE]; printf("Enter a string: "); fgets(str, MAX_SIZE, stdin); printf("The entered string is: %s", str); return 0; }
Output:
In this illustration, we built a character array str with a maximum capacity of MAX_SIZE and read user input using fgets. Printf is then used to output the entered string.
3) Using %[^\n]%*c
The scanf() function allows you to accept a string input in C using the format specifier %[n]%*c.
Here's an example code snippet to illustrate how to use it:
#include <stdio.h> int main() { char str[50]; printf("Enter a string: "); scanf("%[^\n]%*c", str); printf("You entered: %s", str); return 0; }
Output
Here %[n] is used in the code above to search the input until the newline character n is found. The newline character that was left in the input buffer after the initial scanf() function is consumed using the%*c format specifier instead of being read by the following input operation.
Note: %[n] does not add the newline character in the final string despite reading a string that could include whitespace. In order to prevent buffer overflow, make sure the buffer you give has enough space to contain the input string.
4) Using %[^\n]s
The %[n]s format specifier in C can be used to accept input for a string up until a newline character is encountered. Here is a sample of some code that illustrates this:
#include <stdio.h> int main() { char str[100]; printf("Enter a string: "); scanf("%[^\n]s", str); printf("The input string is: %s\n", str); return 0; }
Output:
To store the input string, we declare a character array named str with a capacity of 100 in the code above. The string input is then taken using the scanf() method and the%[n]s format specifier. The%[n]s format specifier instructs the scanf() function to read characters up until a newline character is detected (%[n]), and then save the data as a string with a null termination (%s).
Note: The scanf() method might be risky if the maximum amount of characters to read is not specified, since this could result in a buffer overflow. By declaring the str array with a size of 100 in the example above, we have restricted the input string to a maximum of 100 characters.
Conclusion
In conclusion, you may use the %[n]s format specifier in C to accept input for a string. With the help of this format specifier, you may read characters up until a newline is encountered and then store the data as a string with a null termination. When using the scanf() function to read strings, you should exercise caution because the operation may be hazardous if you don't provide the maximum number of characters to read.