1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char rand(char x);
int main()
{
char input[80] = {0};
char rando[80] = {0};
char choice = 0;
char rando2[80] = {0};
if(strlen(input) >= 10 && strlen(input) <= 80); {
    printf("Please enter a string with 10-80 characters: ");
    scanf("%s", input);
    printf("Orginal string: %s\n", input);
    rando = my_rand(input);
    printf("New string: %s\n", rando);  }
else{
    return 0; }
printf("Would you like to shuffle this string again?(y or n): ");
scanf("%c\n", &choice);
if( choice == 'y') {
    rando2 = my_rand(rando);
    printf("New string: %s\n", rando2);
        }
else if {
    printf("Would you like to shuffle another string?(y or n): ");
    scanf("%c\n", &choice2); }
    if(choice2 == 'y') {
        printf("Please enter a string with 10-80 characters: ");
        scanf("%s", input2);
        printf("Original string: %s\n", input2);
        char rando3 = my_rand(rando2);
        printf("New string: %s\n", rando3); }
    else:

        return 0;
return 0;
}

大家好,我的目标是根据需要多次随机播放用户输入字符串,提示是否继续。我很难弄清楚如何洗牌,有人可以帮忙吗?

这是示例输出:

Please enter a string with 10-80 characters:
initialnaivepassword

Original string: initialnaivepassword

New string: ntlvdiepnaaorsiiiwas

Would you like to shuffle this string again:y

New string: saiiwndrvpaiioneslat

Would you like to shuffle this string again:n

Would you like to shuffle another string? :y
Please enter a string with 10-80 characters:
anothernaivepassword

Original string: anothernaivepassword

New string: svdoanoprhsterneaaiw

Would you like to shuffle this string again:y

New string: eaapnrtwhrosvidosaen

Would you like to shuffle this string again:n

Would you like to shuffle another string? :n
4

4 回答 4

6

这是一些进行改组的代码,希望对您有所帮助:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

void shuffle(char *);

int main(int argc, char *argv[])
{
    char sBuff[1024];
    char sFinish[10];
    srand(time(NULL));

    printf("Enter a string:\n");
    scanf("%s",sBuff);
    do
    {
        shuffle(sBuff);
        printf("\nShuffled string is:\n%s\n\n",sBuff);
        printf("Suffle again? (y/n)\n");
        scanf("%s",sFinish);
    }
    while (strcmp(sFinish,"y") == 0);

    return 0;
}

void shuffle(char *sBuff)
{
    int i, random, length = strlen(sBuff);
    char temp;

    for (i = length-1; i > 0; i--)
    {
        random = rand()%(i+1);
        temp = sBuff[random];
        sBuff[random] = sBuff[i];
        sBuff[i] = temp;
    }
}
于 2013-10-21T14:00:00.007 回答
3
char choice[1] = ""; //wrong declaration. You should either declare a single character or array with two characters 


 rando = rand(input);  // you are passing string here 

所以函数的声明也是错误的

 char rand(char x);

  if else {  // you should use `else if`  not `if else`

并且您使用函数名称rand 不是一个好习惯,因为这是找到的预定义函数 stdlib.h

利用my_rand

于 2013-10-21T13:06:21.030 回答
1

您的代码在许多层面上都是错误的(正如您已经说明它无法编译的事实)。我将注释我能找到的内容:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* useless, you hide a builtin function here, without implementing anything */
char rand(char x);

int main()
{
    char input[80] = "";
    char rando[80] = "";
    char choice[1] = "";
    char rando2[80] = "";

    /* 1. this is always true, as you have just filled the arrays 
     * 2. the semicolon after if means there is no body, so the body's scope is always executed
     */
    if(strlen(input) >= 10 && strlen(input) <= 80); {
        printf("Please enter a string with 10-80 characters: ");
        scanf("%s", input);
        printf("Orginal string: %s\n", input);

        /* input is a char[], rand takes a char */
        rando = rand(input);
        printf("New string: %s\n", rando);  }

    /* you are using else as a label here, probably not allowed */
    else:
        return 0;
    printf("Would you like to shuffle this string again?(y or n): ");
    scanf("%c\n", choice);

    /* y is not declared, so this results in an unknown variable, you probably mean 'y',
     * then again, choice is a char[] not a char
     */
    if( choice == y) {
        rando2 = rand(rando);
        printf("New string: %s\n", rando2);
        }

    /* if else is invalid */
    if else {
        printf("Would you like to shuffle another string?(y or n): ");
        scanf("%c", choice2); }
        if(choice2 == y) {
            printf("Please enter a string with 10-80 characters: ");
            scanf("%s", input2);
            printf("Original string: %s\n", input2);
            char rando3 = rand(rando2);
            printf("New string: %s\n", rando3); }
        else:

    /* no need to return twice */
    return 0;
    return 0;
}

正如我在评论中所说。首先浏览一些基本的 C 教程,了解该语言及其语法。然后返回一段可编译的代码。祝你好运!

于 2013-10-21T13:17:48.740 回答
1

这不是一个解决方案(但非常欢迎您支持它:-D),只是一个长评论。改变你的

1.

char input[80] = "";
char rando[80] = "";
char choice[1] = "";
char rando2[80] = "";

char input[80] = {0}; // Will initialize all 80 char mem to 0
char rando[80] = {0};
char choice = 0;      // As you want only one char as input, no need to use an array.
                      // Just initialize the char to 0
char rando2[80] = {0};

2.

else: // Is not supported in C

else {/* code here*/}

3.

scanf("%c\n", choice);
if( choice == y)

scanf("%c\n", &choice); // choice is no longer an array, 
                        // so we have to pass the address of choice
if( choice == 'y' ) // 'y' is not an int value, its a char literal
于 2013-10-21T13:21:59.547 回答