Hi, I'm back again. Anyways, I have been working on a little project on my own. But I have a question about something. How would jumble characters in a string around. Let's say user inputs a word computer , the program then jumbles the characters in the string to mpcotreu. Is there a way to use a random generator to get a random jumble? I have just started studying strings and functions and the string class is quite broad. I haven't gotten very far, so any input would be thankful.

Hi, I'm back again. Anyways, I have been working on a little project on my own. But I have a question about something. How would jumble characters in a string around. Let's say user inputs a word computer , the program then jumbles the characters in the string to mpcotreu. Is there a way to use a random generator to get a random jumble? I have just started studying strings and functions and the string class is quite broad. I haven't gotten very far, so any input would be thankful.

A string is an array of characters. In your example, let's create a variable called aString and put the word "computer" in it.

string aString = "computer";

"computer" is 8 characters long so the storage of aString is this:

aString[0] = 'c';
aString[1] = 'o'
aString[2] = 'm';
aString[3] = 'p';
aString[4] = 'u';
aString[5] = 't';
aString[6] = 'e';
aString[7] = 'r';

So what you want to do is create an array of 8 integers that contains the integers 0 through 7 in random order. You need to create this array so that each integer from 0 to 7 occurs once and exactly once. Your code is going to be something like this:

aString = "computer";
int randomNum[8];
// code to place 0 through 7 in random order into
// randomNum array.

// display
for (int i = 0; i < aString.length (); i++)
     cout << aString[randomNum[i]];
This article has been dead for over six months. Start a new discussion instead.