[英]How to assign a single character from a string to another string in C?
I have 我有
char alphabet[27] = {"abcdefghijklmnopqrstuvwxyz"};
and I want to assign a random letter from the above string to a string called 我想将上述字符串中的随机字母分配给一个名为
char guess[2]
This is what I have so far: 这是我到目前为止的内容:
srand(time(NULL));
int r = rand() % 28;
char strcpy(guess, alphabet[r]);
I know this won't work because it will try to copy the whole alphabet
into guess
right? 我知道这是行不通的,因为它将尝试将整个
alphabet
复制到guess
对吗? I just want to assign a random letter to guess
so I can create a hangman program and I want the PC to guess the letters to the secret word. 我只想分配一个随机字母来
guess
这样我就可以创建一个hangman程序,并且我希望PC可以猜测到秘密单词的字母。 In a previous version of the program, I was the one to guess the letters so I just used 在该程序的先前版本中,我是猜字母的人,因此我只是使用了
gets(guess);
to do it. 去做吧。 I want it to be the same but with the PC guessing random letters from the
alphabet
string. 我希望它是相同的,但PC可以从
alphabet
字符串中猜测随机字母。 Is that possible? 那可能吗?
Just forgo any form of strcpy
: 只要放弃任何形式的
strcpy
:
srand(time(NULL));
int r = rand() % 26;
guess[0] = alphabet[r];
guess[1] = '\0';
But if you want to use it just as a char
, and not an array, just declare char guess;
但是,如果要仅将其用作
char
而不是数组,则只需声明char guess;
and use it as such. 并按原样使用它。
I know this won't work because it will try to copy the whole alphabet into guess right?
我知道这是行不通的,因为它将尝试将整个字母复制到猜测中,对吗?
Right on the "wouldn't work", but not exactly right on the why part: it will try using alphabet[r]
as an address , which will cause undefined behavior. 正确的“行不通”,但不完全正确,为什么原因:它将尝试使用
alphabet[r]
作为地址 ,这将导致未定义的行为。
Getting a single char
is much simpler than that - you do not need a function, you can do this: 获取单个
char
比这简单得多-您不需要功能,可以执行以下操作:
char guess = alphabet[r];
That's it - a single char
can be copied with a simple assignment. 就是这样-一个简单的任务就可以复制一个
char
。
In a previous version of the program, I was the one to guess the letters so I just used
gets(guess)
在该程序的先前版本中,我是猜字母的人,因此我只使用了
gets(guess)
This explains why you declared guess
as char[2]
, not as char
. 这解释了为什么您将
guess
声明为char[2]
而不是char
。 You do not need to do it like this in this version of the program, because you are not reading the input, so you don't need an extra spot for the null terminator. 在此版本的程序中,您不需要这样做,因为您不需要读取输入,因此不需要为空终止符添加额外的位置。
Also note that gets
should not be used - it is inherently unsafe, because it can easily cause buffer overruns. 还要注意的是
gets
不应该使用-它本质上是不安全的,因为它很容易造成缓冲区溢出。 You should use fgets
instead, because you can pass the size of the buffer to fgets
, and ensure that it wouldn't go past the end of your buffer. 您应该改用
fgets
,因为您可以将缓冲区的大小传递给fgets
,并确保它不会超出缓冲区的末尾。
Why not just do something like 为什么不做类似的事情
guess[0] = alphabet[r];
? ?
Just as you can read a single character from the array alphabet
with alphabet[index]
you can set a single character in the same way as well, so: 正如您可以从带有
alphabet[index]
的数组alphabet
读取单个字符一样,您也可以用相同的方式设置单个字符,因此:
guess[0] = alphabet[r];
To make sure guess can be used as a NUL-terminated string you'll need to make sure the second char is NUL: 为了确保可以将猜测用作NUL终止的字符串,您需要确保第二个字符为NUL:
guess[1] = '\0';
A final bug in your code is in the following line: 您的代码中的最后一个错误在以下行中:
int r = rand() % 28;
The 28 needs to be replaced by 26 (there are only 26 valid values). 需要将28替换为26(只有26个有效值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.