My main function contains a string that I would like to jumble. I have found a piece of code which passes in a char* and returns 0 when complete. I have followed the code providers instructions to simply pass in the string which you would like to have jumbled.
#include <iostream>
#include <string.h>
#include <time.h>
using namespace std;
int scrambleString(char* str)
{
int x = strlen(str);
srand(time(NULL));
for(int y = x; y >=0; y--)
{
swap(str[rand()%x],str[y-1]);
}
return 0;
}
When I do this, I recieve an error that "no suitable conversion function "std::string" to "char*" exists.
I have also tried passing in a const char* but that won’t allow me to access the word and change it.
Why are you mixing
std::stringwithchar*? You should just operate directly on the string:Usage becomes:
That being said, std::random_shuffle will do this for you…