C语言 如何从C中的用户输入读取字符串,放入数组并打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19254121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:39:57  来源:igfitidea点击:

How to read a string from user input in C, put in array and print

carraysinputuser-input

提问by Burbanana

I'm fairly new to C and I've read a few chapters of a C book I have and now I have to make an assignment but I am all confused hope someone can help me out.

我对 C 相当陌生,我已经阅读了我所拥有的 C 书的几章,现在我必须做一个作业,但我很困惑,希望有人能帮助我。

I have to read 2 strings from user (char arrays) input with a max length of 100 characters and convert them to capital letters and print them out with a newline \n after each word.

我必须从用户(字符数组)输入中读取 2 个字符串,最大长度为 100 个字符,并将它们转换为大写字母,并在每个单词后用换行符 \n 打印出来。

Until now I have this:

直到现在我有这个:

int main() {
char chars[100];
int i = 0;
char str1;
char str2;
int j = 0;

scanf("\n %c", str1);
scanf("\n %c", str2);


while (str1[i] != '
scanf("\n %c", str1);
') { chars[i] = str1[i]; toupper(chars[i]); printf(chars[i]); i++; } while (str2[j] != '
fgets(str1, 100, stdin);
') { chars[j] = str2[j]; toupper(chars[j]); printf(chars[j]); j++; } return 0; }

after it takes the 2 inputs from user, it says stops running and says run failed.

在接受用户的 2 个输入后,它说停止运行并说运行失败。

回答by Tim Pierce

This is your immediate problem:

这是您的直接问题:

chars[i] = toupper(chars[i]);

This format string tells scanfto read some amount of whitespace followed by a single character, which is to be stored in str1.

此格式字符串告诉scanf读取一定数量的空格,后跟单个字符,该字符将存储在str1.

@H2CO3 is right: scanfis almost never the right answer for reading input from the user. It performs no bounds checking and is too finicky about the format of user input; one unexpected character will completely confuse it. You are very strongly encouraged to use fgetsinstead if at all possible:

@H2CO3 是对的:scanf几乎从来都不是读取用户输入的正确答案。它不执行边界检查,并且对用户输入的格式过于挑剔;一个意想不到的角色会完全混淆它。fgets如果可能的话,强烈建议您改用:

char str1[101];
char str2[101];
scanf("%100[^\n]",str1);
scanf("%100[^\n]",str2);

If you are not allowed to do this, you should ask your professor why not. Seriously.

如果你不被允许这样做,你应该问你的教授为什么不这样做。严重地。

If you absolutely must use scanfto read input here, you can do it the way @user2479209 described in their answer.

如果您绝对必须使用scanf这里读取输入,您可以按照@user2479209 在他们的回答中描述的方式来完成。

There is another problem with your program, which is that toupper(chars[i])will not change the value stored in chars[i]. You have to assign the result of toupperback to the array explicitly, e.g.:

您的程序还有另一个问题,即toupper(chars[i])不会更改存储在chars[i]. 您必须将toupper返回的结果显式分配给数组,例如:

// Symbolic constants
#define SIZE 101

char str1[SIZE];
char str2[SIZE];
scanf_s("%100s%100s", str1, SIZE, str2, SIZE);

回答by sha

change str1 and str2 to array. use %100[^\n] in first two scanf's.It's a regular expression trick to read strings with spaces.read about it.

将 str1 和 str2 更改为数组。在前两个 scanf 中使用 %100[^\n]。这是读取带有空格的字符串的正则表达式技巧。阅读有关它的信息。

scanf_s("%100s", str1, SIZE);
scanf_s("%100s", str2, SIZE);

This will take in two strings.hit enter after entering the first string to enter the second.

这将接受两个字符串。输入第一个字符串后按回车键进入第二个。

回答by angelita

I think that you can also use scanf_s(secure scanf) and collect both strings input by user in one shot. You can also define the array size as a symbolic constant for flexibility, on the top of your program, outside of mainand below the #includepreprocesor directives.

我认为您还可以使用scanf_s(secure scanf) 并一次性收集用户输入的两个字符串。您还可以在程序顶部、预处理器指令的外部main和下方将数组大小定义为符号常量以提高灵活性#include

scanf_s("%100s", str1, 101);
scanf_s("%100s", str2, 101);

Alternatively:

或者:

##代码##

I think that, if you don't want to do #define SIZE 101, you can also do:

我认为,如果你不想做#define SIZE 101,你也可以这样做:

##代码##

I'm new to C so feel free to improve this answer.

我是 C 的新手,所以请随时改进这个答案。