Word Jumble

Can somebody explain this part to me. I'm suppose to write a word jumble program that jumbles the words to how long the word is. So if the word is 7 letters it jumbles it 7 times. This is what the book gives me but it does not explain it that well. I've debugged it to see what's going on but I still cant why it does what it does

1
2
3
4
5
6
7
8
9
10
  string jumble = theWord;
	int length = jumble.size();
	for (int i = 0; i < length; ++i)
	{
		int index1 = (rand() % length);
		int index2 = (rand() % length);
		char temp = jumble[index1];
		jumble[index1] = jumble[index2];
		jumble[index2] = temp;
	}
index1 and index2 are simply random numbers >= 0 and < the length of the word. lines 7-9 simply swap two positions of the word determined by index1 and index 2. Line 3 is a for loop that will repeat this process length times, where length is the length of the word.
so if the word is 'jumble', and index1 = 5 and index2=0 would it switch the e and the j creating 'eumblj'?

is that how it works?
Correct. It would then repeat that process 5 more times.
alright thank you
Topic archived. No new replies allowed.