Reputation: 69
#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
Upvotes: 1
Views: 4140
Reputation: 11840
Here's some code that does the shuffling, hope it's helpful:
#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;
}
}
Upvotes: 6
Reputation: 5389
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
Upvotes: 1
Reputation: 33491
Your code is wrong on many levels (as you state already in the fact that it doesn't compile). I'll annotate what I can find:
#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;
}
As I said in a comment. Walk through some basic C tutorials first, get to know the language and its syntax. Then come back with a compilable piece of code. Good luck!
Upvotes: 1
Reputation: 10516
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
so the declaration of function is also wrong
char rand(char x);
if else { // you should use `else if` not `if else`
and you are using function name rand
is not good practice as this is predefined function found stdlib.h
.
use my_rand
Upvotes: 3