Advertisement
Lawnknome

stringmix2

Nov 6th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <ctime>
  4. #include <string>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. void stringMix(char string1[], char string2[]);
  9.  
  10. int main ()
  11. {
  12.     char string1[50];
  13.     char string2[50];
  14.     char again;
  15.    
  16.     srand(time(0));
  17.    
  18.     cout << "Please input a string to randomize." << endl;
  19.     cin.getline(string1, 50);
  20.    
  21.     do{
  22.    
  23.         stringMix(string1, string2);
  24.        
  25.         cout << "Would you like to randomize this string again? (Y/N)";
  26.         cin >> again;
  27.    
  28.     }while (again == 'Y' || again == 'y'); 
  29.        
  30.     return 0;
  31. }  
  32.  
  33. void stringMix(char str1[50], char str2[50])
  34. {  
  35.     int length;
  36.     length = strlen(str1);
  37.     int position;  
  38.  
  39.     for(int j = 0; j < length; j++)
  40.     {
  41.         str2[j] = 0;
  42.     }
  43.  
  44.     for(int i=0; i < length; i++)
  45.     {  
  46.         do
  47.         {
  48.         position = rand()% length;
  49.         }
  50.  
  51.         while(str2[position] != 0);
  52.        
  53.         str2[position] = str1[i];  
  54.            
  55.     }      
  56.        
  57.     cout << "\nThe randomization of " << str1 << " is " << str2 << endl;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement