通过随机播放用户输入的字符串 [英] Shuffle a string input by user

查看:131
本文介绍了通过随机播放用户输入的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
焦炭兰特(字符x)的;
诠释的main()
{
字符输入[80] = {0};
焦兰多[80] = {0};
字符选择= 0;
焦炭rando2 [80] = {0};
如果(strlen的(输入)GT; = 10&放大器;&放大器; strlen的(输入)下; = 80); {
    的printf(请输入一个字符串的字符10-80:);
    scanf函数(%S,输入);
    的printf(原单字符串:%S \\ n,输入);
    兰多= my_rand(输入);
    的printf(新的字符串:%S \\ n,兰多); }
其他{
    返回0; }
的printf(你想重新洗牌这个字符串(Y或N)?);
scanf函数(%C \\ n,&安培;选择);
如果(选择=='Y'){
    rando2 = my_rand(兰多);
    的printf(新的字符串:%S \\ n,rando2);
        }
否则,如果{
    的printf(你想洗牌另一个字符串(Y或N)?);
    scanf函数(%C \\ n,&安培;选择2); }
    如果(选择2 =='Y'){
        的printf(请输入一个字符串的字符10-80:);
        scanf函数(%S,输入2);
        的printf(原始字符串:%S \\ n,输入2);
        焦炭rando3 = my_rand(rando2);
        的printf(新的字符串:%S \\ n,rando3); }
    其他:        返回0;
返回0;
}

您好,大家好,我的目标是重新洗牌的用户输入的字符串很多次,因为他们想,促使是否继续还是没有。我有一个艰难的时间搞清楚如何洗牌字符串,任何人都可以伸出援助之手?

这是示例输出:

 请输入字符串的字符10-80:
initialnaivepassword原始的字符串:initialnaivepassword新的字符串:ntlvdiepnaaorsiiiwas你想重新洗牌这个字符串:Y新的字符串:saiiwndrvpaiioneslat你想重新洗牌这个字符串:N你想洗牌另一个字符串? :Y
请10-80个字符输入一个字符串:
anothernaivepassword原始的字符串:anothernaivepassword新的字符串:svdoanoprhsterneaaiw你想重新洗牌这个字符串:Y新的字符串:eaapnrtwhrosvidosaen你想重新洗牌这个字符串:N你想洗牌另一个字符串? :N


解决方案

这是不是一个解决方案(但你最欢迎给予好评它:-D),只是一个长注释。
更改

1

 字符输入[80] =;
焦兰多[80] =;
字符的选择[1] =;
炭rando2 [80] =;

 字符输入[80] = {0}; //将初始化所有80字符纪念品为0
焦兰多[80] = {0};
字符选择= 0; //当你只需要一个字符作为输入,无需使用数组。
                      //初始化刚刚炭为0
焦炭rando2 [80] = {0};

2

 其他://是不是在C支持

 其他{/ * code这里* /}

3。

  scanf函数(%C \\ N的选择);
如果(选择== Y)

  scanf函数(%C \\ n,&安培;选择); //选择不再是一个阵列,
                        //所以我们要通过选择的地址
如果(选择=='Y')//'Y'不是一个int值,它的一个char字面

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

Hello guys, my goal is to shuffle a user input string as many times as they would like, prompting whether to keep going or not. I am have a tough time figuring out how to shuffle the string, can anyone lend a hand?

This is the sample output:

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

解决方案

This is not a solution ( but you are most welcome to upvote it :-D ), just a long comment. Change your

1.

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

To

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

To

else {/* code here*/}

3.

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

To

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

这篇关于通过随机播放用户输入的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆