簡體   English   中英

隨機播放用戶輸入的字符串

[英]Shuffle a string input by user

#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

這是一些進行改組的代碼,希望對您有所幫助:

#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;
    }
}
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

您的代碼在許多級別上都是錯誤的(正如您已經陳述的事實那樣,它無法編譯)。 我將注釋我可以找到的內容:

#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教程,了解該語言及其語法。 然后返回一段可編譯的代碼。 祝好運!

這不是一個解決方案(但是,我們非常歡迎您對它進行投票:-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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM