I have to write a function that takes a string as an input and displays a jumbled version of that string. I've asked user input for the string already in the main function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void jumbleString (string& str)
{
int x = str.length();//gets length of string
for (int i = x; i>0; i--)
{
//randomly jumbles string
int pos = rand()%x;
char tmp = str[i-1];
str[i-1] = str[pos];
str[pos] = tmp;
}
return;
}
And then in the main function, I called the jumble function and tried to display the output. Here's part of the code of where I call the function.
if (choice == '4')
{
// this is just the declaration of the function jumbleString
void jumbleString(string& str);
jumbleString(str) ; // this actually calls the function
// this prints the address of the string str
cout << &str << endl;
// this prints the contents of the string str
std::cout << str << '\n' ;
}
Pass the string by value to the function (the function gets a copy of the string) and let it return the jumbled string. std::string jumbleString( std::string str )