How to input a string in the C language?
In C language, you can input a string using either the gets() function or the scanf() function.
Using the gets() function:
#include <stdio.h>
int main() {
char str[50];
printf("请输入一个字符串:");
gets(str);
printf("您输入的字符串是:%s\n", str);
return 0;
}
Utilize the scanf() function:
#include <stdio.h>
int main() {
char str[50];
printf("请输入一个字符串:");
scanf("%s", str);
printf("您输入的字符串是:%s\n", str);
return 0;
}
It is important to note that the scanf() function stops reading when it encounters a space or newline character, so if you need to input a string with spaces, it is recommended to use the gets() function. Additionally, to avoid inputting strings that exceed the length of an array, you can use the fgets() function as a replacement for gets().