如何在C中的字符串中随机播放字符

时间:2021-04-18 20:17:48

I am creating a program and part of it needs to take a word, and jumble the letters. I know that there is officially no string data type within C, so technically the characters in the word are already in array? They just need sorting. (That is my understanding anyway). I also know that C isn't very good for actual random numbers, I normally use the time as the seed, not sure if this would affect shuffling the letters.

我正在创建一个程序,其中一部分需要接受一个字,并混杂字母。我知道C中正式没有字符串数据类型,所以从技术上讲,单词中的字符已经在数组中了?他们只需要排序。 (无论如何,这是我的理解)。我也知道C对于实际的随机数并不是很好,我通常使用时间作为种子,不确定这是否会影响改组字母。

For instance:

例如:

The word Hello
Split into Characters H/E/L/L/O
Shuffled E/L/O/H/L
New word Elohl

Hello分裂成字符H / E / L / L / O洗牌E / L / O / H / L新单词Elohl

2 个解决方案

#1


2  

technically the characters in the word are already in array?

从技术上讲,单词中的字符已经在数组中?

You can treat them like a null-terminated array of characters. Apply your favorite shuffle algorithm to the portion of the string between 0, inclusive, and strlen(str), exclusive to produce a shuffled string.

您可以将它们视为以null结尾的字符数组。将您喜欢的shuffle算法应用于0(包括)和strlen(str)之间的字符串部分,独占以生成混洗字符串。

The only catch here is that not all strings can be shuffled in place. Specifically, strings representing string literals are not writable. Trying to change them would lead to undefined behavior.

这里唯一的问题是并非所有字符串都可以在适当的位置进行洗牌。具体来说,表示字符串文字的字符串是不可写的。试图改变它们会导致未定义的行为。

For example, if you do

例如,如果你这样做

char *word = "hello";
shuffle(word);

and try to modify word's characters inside shuffle, you would get undefined behavior. You need to copy the content into a writable array before you can shuffle the content - for example, like this:

并尝试修改shuffle中的单词字符,你会得到未定义的行为。您需要将内容复制到可写数组中,然后才能对内容进行随机播放 - 例如,像这样:

char word[] = "hello";
shuffle(word);

#2


0  

int compare(const void *a, const void *b){
        return *(const char *)a - *(const char *)b;
    }

    char arr[] = "dbaurjvgeofx";

    printf("Unsorted: %s\n", arr);
    qsort(arr, strlen(arr), 1, compare);
    printf("Sorted: %s\n", arr);

#1


2  

technically the characters in the word are already in array?

从技术上讲,单词中的字符已经在数组中?

You can treat them like a null-terminated array of characters. Apply your favorite shuffle algorithm to the portion of the string between 0, inclusive, and strlen(str), exclusive to produce a shuffled string.

您可以将它们视为以null结尾的字符数组。将您喜欢的shuffle算法应用于0(包括)和strlen(str)之间的字符串部分,独占以生成混洗字符串。

The only catch here is that not all strings can be shuffled in place. Specifically, strings representing string literals are not writable. Trying to change them would lead to undefined behavior.

这里唯一的问题是并非所有字符串都可以在适当的位置进行洗牌。具体来说,表示字符串文字的字符串是不可写的。试图改变它们会导致未定义的行为。

For example, if you do

例如,如果你这样做

char *word = "hello";
shuffle(word);

and try to modify word's characters inside shuffle, you would get undefined behavior. You need to copy the content into a writable array before you can shuffle the content - for example, like this:

并尝试修改shuffle中的单词字符,你会得到未定义的行为。您需要将内容复制到可写数组中,然后才能对内容进行随机播放 - 例如,像这样:

char word[] = "hello";
shuffle(word);

#2


0  

int compare(const void *a, const void *b){
        return *(const char *)a - *(const char *)b;
    }

    char arr[] = "dbaurjvgeofx";

    printf("Unsorted: %s\n", arr);
    qsort(arr, strlen(arr), 1, compare);
    printf("Sorted: %s\n", arr);