HOW TO SHUFFLE A WORD?

HOW TO SHUFFLE A WORD COMING FROM A FILE?


FILE *DOCU;
DOCU = fopen ("file.txt", "r");
if (DOCU == 0)
{
printf ("\nFailed to open!\n");
exit (1);
}
else
{
 for(a=0; a<100; a++)
 {
 	if(!feof(DOCU))
 	 fscanf(DOCU, "\n%s",&string[a]);
 	 }
fclose (DOCU);
}
x = rand()% 100;


then what's next? please help me. :(
continue these codes.
Why are you spamming the forum with the same question in multiple threads?

You already received some answers to this in www.cplusplus.com/forum/general/111465/
coz no one answering my question..
okay,
how to delete topic?
Last edited on
closed account (3qX21hU5)
Take the word coming from the file and put it in a std::string. You then will call random shuffle to shuffle the word at random.

For example

1
2
std::string name("Brandon");
std::random_shuffle(name.begin(), name.end());


Now you can also pass a generator to the function like so to make a bit more random.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    std::srand(std::time(0));

    std::string name("Brandon");
    std::random_shuffle(name.begin(), name.end(), [] (int i) {return std::rand() % i;});

    std::cout << name;

    return 0;
}


Now there is also the new std::shuffle() which came with C++11 that you can use also which is just like random shuffle except it works with a generator from the random header.
Last edited on
coz no one answering my question..


That's not true. You got answers in that thread.

But even if it were true, that's not a reason to spam more threads. At most, you could bump your previous thread.
Topic archived. No new replies allowed.