0

Hello fellow programmers, I was given a homework assignment to write code that takes a string entered by the user who then chooses how to manipulate the string and in certain cases print it to the console. I have the code mostly written and working but there are a couple functions I don't understand how to write.

One of the options is supposed to take the string and show a "jumbled version" of the string without actually changing it. (eg. "Hello World!" becomes "oleWrl !odlH" or any other random variation each time the option is chosen.)

This is the function I have now and was not accepted by my professor because it changes the original string itself.

std::string jumbleString(string str2) { //jumble
  string str = str2;
  random_shuffle(str.begin(), str.end());

  return str;
}

What is an alternate way I can jumble/shuffle and print a string to get the same results?

edit: Added an actual question

I apologize for any formatting irregularities, this is my first time posting here. Thank you for any and all help. :) This assignment is driving me nuts.

Max Orozco
  • 33
  • 1
  • 8
  • 4
    No it doesn't change the original. Your professor is very poorly articulating that they want you to use some form of a loop. Your way is the way most c++ savvy programmers will approach the program, so take comfort in that. – StoryTeller - Unslander Monica Nov 04 '16 at 22:49
  • 1
    What @StoryTeller said is very true. Using the standard library's algorithms to replace hand-coded loops is generally very welcome in modern C++ code. – Daniel Kamil Kozar Nov 04 '16 at 22:54
  • 1
    Having said the above... This is a Q&A site, and your post doesn't actually contain a question. – StoryTeller - Unslander Monica Nov 04 '16 at 22:55
  • I apologize, I meant to ask for an alternate way that I might be able to do this. I was more focused on the formatting lmao @StoryTeller I do take comfort in what you told me, by the way. Thanks, ha. – Max Orozco Nov 04 '16 at 23:52
  • Interesting fun fact: In `std::string jumbleString(string str2)` parameter `str2` is passed by value. [What's the difference between passing by reference vs. passing by value?](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) Good question. Read the link and find out. Anyway, this most likely means `str2` is already a copy of the source so `string str = str2;` accomplishes nothing useful. You can safely operate on `str2` throughout the function. Read the above as Your professor has consumed an excessive amount of crack. – user4581301 Nov 05 '16 at 00:03
  • Question: Are all of the words in the string supposed to be shuffled individually or is the string one big pool to be shuffled? – user4581301 Nov 05 '16 at 00:15
  • @user4581301 As I understand it, it's supposed to be one big pool. Also, thanks for the tip. – Max Orozco Nov 05 '16 at 00:24

1 Answers1

0

Well, you need to map each character to a new position... so map each original position to a new position:

using size_type = std::string::size_type;

std::vector<size_type> pos(str2.length());
size_type n = 0;
std::generate(begin(pos), end(pos), [&]{ return n++; });
std::shuffle(begin(pos), end(pos), std::rand);
for (auto i : pos)
  std::cout << str2[i];
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458