【发布时间】:2015-05-05 17:26:47
【问题描述】:
在主要学习 Java 之后,我本学期刚开始学习 C,我被要求创建一个程序,允许用户输入字符串,然后打乱字符串中的字母。
#include <stdio.h> //Alows input/output operations
#include <stdlib.h> //Standard utility operations
int main(int argc, char *argv[]) //Main method
{
printf("------------------------\n");
printf("WELCOME TO THE SCRAMBLER\n");
printf("------------------------\n\n");
char userString[50]; //Declaring the user string
printf("Please input a String :> "); //Iforms the user to enter a string
scanf("%s", userString); //Allows the user to enter in a string
char targetLetter[2]; //Declaring the target letter
char replaceLetter[2]; //Declaring the replace letter
while(1)
{
}
}
这是我目前所拥有的,我只需要有关如何实际打乱字符串的帮助/建议。用户应该能够随意加扰字符串,直到他们输入特定字符,然后程序终止。提前感谢您的帮助!
【问题讨论】:
-
如果打乱是指将字母随机排列,您可以使用fisher-yates shuffle。
-
@indiv 感谢您的回复。我的目标是让用户可以从他们创建的字符串中选择一个字母并将其替换为另一个字母/字符。
-
This 你会感兴趣的。