#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
答案 0 :(得分:5)
这里有一些代码可以进行改组,希望它有用:
#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;
}
}
答案 1 :(得分: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
答案 2 :(得分: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教程,了解语言及其语法。然后回来一段可编辑的代码。祝你好运!
答案 3 :(得分: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