~this code is in c++
~you need not change anything in the code
~the commented lines are there just in case you want to see how the program works in real time - simply uncomment them and run the program, these commented lines are not necessary for the program to work
~code does not swap the characters of the string n times just to randomize
~string contents are shuffled character by character only
~the code is done with Code::Blocks
HOW THE CODE WORKS:
~the program asks user to input a string, then it outputs the shuffled version of the string
1. the program tells the user to enter a string
2. getline(cin,word); saves the string into string variable word
3. the length of the string is determined and saved to int variable x
4. srand is called so that the program will randomize differently for each instance you run the program
5. the while loop does the assigning of characters found in string variable word to temp
6. the resulting string temp is outputted so you can see the result of the shuffle
~further explanation for number 5
for example, you entered "twister"
twister is saved to string variable word
x is given the value of the length of the string variable word, which in this example, is 7 (seven)
in the loop,
temp is the string where we add characters found in string variable word. the character that we will use and copy to temp is determined by the int variable y, which generates a value from 0(zero) to the length of the string variable minus one, which in this instance, is 6. after choosing the character, the character is appended to temp.
the problem now is, we dont want that same character to be used again/selected by the randomized selector y - this problem is solved by the other lines in our program.
the idea is, we remove that chosen character (after appending it to temp) from the original string word, so it would not be chosen again.
we can remove that character by:
1.) cutting the string into three, first part will be from the start of the string to the character before the chosen character, second part will be the chosen character, then the last part will be after the chosen character to the last of the string... after cutting it and saving the first and third(last) parts to new strings, we fuse them into one....or
2.) simply assigning the last character of the string into the place of the chosen character (which will overwrite and delete the chosen character) , then cutting the string from the beginning up to the second-to-the-last part of the string (which will delete the last character of the string - which is okay since we moved it to the chosen character's place already)
after that, you see, we don't have the chosen character anymore, we have the last character of the string on it, and the length of the string decreased by one.
the loop then continues until all of the characters from string variable word is gone.
~actually from what i tested, there are no restrictions for the program input...
~it even accepts strings with spaces
~there are no restrictions for values of the string variable word too.
uhm i think thats all about the code. i don't really know if this kind of code exists some place else other than this post because all i see are either random swapping shufflers or threads that needs help on shuffling. and just so you know, it is me who made this code, so yes its my mistake if ever there is one in the code.