Advertisement
Guest User

CodeNeverReaches

a guest
Mar 9th, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6. const int MAX_SIZE = 100;
  7. string words[MAX_SIZE];
  8. int counter = 0;
  9.  
  10. void print_permutations(string s)
  11. {
  12.     bool exists = true;
  13.     int i=0;
  14.     int j=0;
  15.     srand(time(NULL));
  16.  
  17.     for(int k = 0; k < s.length()/2; ++k)
  18.     {
  19.         i = rand()%s.length();//-1;
  20.         j = rand()%s.length();//-1
  21.         /*if (i < 0)
  22.             i = 0;
  23.         if (j < 0)
  24.             j = 0;
  25.         if (i == j)
  26.              i++;
  27.         if (i > s.length()-1)
  28.             i = 0;*/
  29.         if(i != j){
  30.             s[i] = s[i] ^ s[j];
  31.             s[j] = s[i] ^ s[j];
  32.             s[i] = s[i] ^ s[j];
  33.         }
  34.     }
  35.  
  36.     if(exists == true)
  37.     {
  38.         for(int m = 0; m < MAX_SIZE; m++)
  39.         {
  40.             if(words[m] == s)
  41.             {
  42.                 cout << "Permutation exists" << endl;
  43.             }
  44.             break;
  45.         }
  46.     }else
  47.         {
  48.             words[counter] = s;
  49.             cout << "permutation: " << s << endl;;
  50.         }
  51. }
  52.  
  53.  
  54. int main()
  55. {
  56.     string word;
  57.     do{
  58.         cout << "\nPlease enter a word" << endl << "Original Word: ";
  59.         cin >> word;
  60.         print_permutations(word);
  61.         counter++;
  62.     }while(word != "1");
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement