Advertisement
Lawnknome

wordshift.cpp

Nov 9th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. using namespace std;
  7.  
  8.  
  9. void shiftR (char sentence[], int shift, int length);
  10. void shiftL (char sentence[], int shift, int length);
  11. void reverse (char sentence[], int length);
  12. void swap (char sentence[], int a, int b);
  13.  
  14.  
  15. int main ()
  16. {
  17.     const int LENGTH = 50;
  18.     char sentence[LENGTH];
  19.     int length;
  20.     string menu;
  21.     int shift;
  22.  
  23.     cout << "Please enter the string you wish to manipulate.\n";
  24.     cin.getline(sentence, LENGTH);
  25.     length = strlen(sentence);
  26.  
  27.     cout << "\n\n\nWhat manipulation would you like to perform on your string?" << endl;
  28.     cout << "You have 4 choices:\n";
  29.     cout << "\"R(x)\" will shift all characters in the string (x) positions right.\n";
  30.     cout << "\"L(x)\" will shift all characters in the string (x) positions left.\n";
  31.     cout << "\"rev\" will reverse the string.\n";
  32.     cout << "\"quit\" will end the program.\n";
  33.  
  34.     do
  35.     {
  36.  
  37.         cout << "Please enter a command: ";
  38.         cin >> menu;
  39.  
  40.         if(menu.at(0) == 'L')
  41.         {
  42.             shift = atoi((menu.substr(1, string::npos).c_str()));
  43.             shiftL(sentence, shift, length);
  44.         }  
  45.            
  46.         else if(menu.at(0) == 'R')
  47.         {
  48.             shift = atoi((menu.substr(1, string::npos).c_str()));
  49.             shiftR(sentence, shift, length);
  50.         }  
  51.  
  52.         else if((menu.at(0) == 'r') && (menu.at(1) == 'e') && (menu.at(2) == 'v'))
  53.            
  54.             reverse(sentence, length);
  55.  
  56.         else if((menu.at(0) == 'q') && (menu.at(1) == 'u') && (menu.at(2) == 'i') && (menu.at(3) == 't'))
  57.            
  58.             return 0;
  59.  
  60.         else
  61.  
  62.             cout << "ERROR: Please enter the menu selection as indicated exactly.\n";  
  63.  
  64.     }while(1); 
  65.    
  66.     return 0;
  67. }
  68.  
  69.  
  70. void shiftR (char sentence[], int shift,int length)
  71. {
  72.    
  73.    
  74.     for(int j = 0; j < shift; j++)
  75.     {  
  76.         char last = sentence[length -1];
  77.  
  78.         for(int i = (length-1); i >= 1; i--)
  79.         {
  80.             sentence[i] = sentence[i - 1];
  81.         }
  82.  
  83.         sentence[0] = last;
  84.     }
  85.        
  86.        
  87.     cout << sentence << endl;  
  88. }
  89.  
  90. void shiftL (char sentence[], int shift, int length)
  91. {
  92.    
  93.     for(int j = 0; j < shift; j++)
  94.     {  
  95.         char first = sentence[0];
  96.  
  97.         for(int i = 0; i < length-1; i++)
  98.         {
  99.             sentence[i] = sentence[i + 1];
  100.         }  
  101.  
  102.         sentence[length-1] = first;
  103.     }
  104.        
  105.     cout << sentence << endl;
  106. }
  107.  
  108. void reverse (char sentence[], int length)
  109. {
  110.     int a, b;
  111.  
  112.     for(a = 0; a < (length/2); a++)
  113.     {
  114.         b = length - a - 1;
  115.         swap(sentence, a, b);
  116.     }  
  117.  
  118.     cout << sentence << endl;
  119. }
  120.  
  121. void swap(char sentence[], int a, int b)
  122. {
  123.     char temporary = sentence[a];
  124.    
  125.     sentence[a] = sentence[b];
  126.     sentence[b] = temporary;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement