Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <stdlib.h>
- #include <string.h>
- using namespace std;
- void shiftR (char sentence[], int shift, int length);
- void shiftL (char sentence[], int shift, int length);
- void reverse (char sentence[], int length);
- void swap (char sentence[], int a, int b);
- int main ()
- {
- const int LENGTH = 50;
- char sentence[LENGTH];
- int length;
- string menu;
- int shift;
- cout << "Please enter the string you wish to manipulate.\n";
- cin.getline(sentence, LENGTH);
- length = strlen(sentence);
- cout << "\n\n\nWhat manipulation would you like to perform on your string?" << endl;
- cout << "You have 4 choices:\n";
- cout << "\"R(x)\" will shift all characters in the string (x) positions right.\n";
- cout << "\"L(x)\" will shift all characters in the string (x) positions left.\n";
- cout << "\"rev\" will reverse the string.\n";
- cout << "\"quit\" will end the program.\n";
- do
- {
- cout << "Please enter a command: ";
- cin >> menu;
- if(menu.at(0) == 'L')
- {
- shift = atoi((menu.substr(1, string::npos).c_str()));
- shiftL(sentence, shift, length);
- }
- else if(menu.at(0) == 'R')
- {
- shift = atoi((menu.substr(1, string::npos).c_str()));
- shiftR(sentence, shift, length);
- }
- else if((menu.at(0) == 'r') && (menu.at(1) == 'e') && (menu.at(2) == 'v'))
- reverse(sentence, length);
- else if((menu.at(0) == 'q') && (menu.at(1) == 'u') && (menu.at(2) == 'i') && (menu.at(3) == 't'))
- return 0;
- else
- cout << "ERROR: Please enter the menu selection as indicated exactly.\n";
- }while(1);
- return 0;
- }
- void shiftR (char sentence[], int shift,int length)
- {
- for(int j = 0; j < shift; j++)
- {
- char last = sentence[length -1];
- for(int i = (length-1); i >= 1; i--)
- {
- sentence[i] = sentence[i - 1];
- }
- sentence[0] = last;
- }
- cout << sentence << endl;
- }
- void shiftL (char sentence[], int shift, int length)
- {
- for(int j = 0; j < shift; j++)
- {
- char first = sentence[0];
- for(int i = 0; i < length-1; i++)
- {
- sentence[i] = sentence[i + 1];
- }
- sentence[length-1] = first;
- }
- cout << sentence << endl;
- }
- void reverse (char sentence[], int length)
- {
- int a, b;
- for(a = 0; a < (length/2); a++)
- {
- b = length - a - 1;
- swap(sentence, a, b);
- }
- cout << sentence << endl;
- }
- void swap(char sentence[], int a, int b)
- {
- char temporary = sentence[a];
- sentence[a] = sentence[b];
- sentence[b] = temporary;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement