How do you input a string in the C language?
In the C language, we can use the scanf function to input a string.
char str[100]; // 声明一个字符数组存储字符串
printf("请输入字符串:");
scanf("%s", str); // 输入字符串并存储到str数组中
printf("您输入的字符串是:%s\n", str); // 输出输入的字符串
It is important to note that the scanf function stops reading when it encounters a space or enter while reading a string, so if you need to input a string containing spaces, you can use the fgets function instead.
char str[100]; // 声明一个字符数组存储字符串
printf("请输入字符串:");
fgets(str, sizeof(str), stdin); // 输入字符串并存储到str数组中
printf("您输入的字符串是:%s\n", str); // 输出输入的字符串
When using the fgets function, you need to specify the size of the array to store the string and pass stdin as the third parameter.