Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits>
  3. using namespace std;
  4.  
  5. // prototypes
  6. string encode(int num, string orig);
  7. void printOutput(string output);
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     cout << "Enter number" << '\n';
  12.     int k = 0;
  13.     cin >> k;
  14.     cout << "Enter text" << '\n';
  15.     // The number of times we are going to interate the characters in the argument of the program
  16.     string text;
  17.     // Get user input of the string we will encode
  18.     cin.ignore (numeric_limits<streamsize>::max(), '\n');
  19.     getline (cin,text);
  20.     printOutput(encode(k,text));
  21.     return 0;
  22.    
  23. }
  24.  
  25. string encode(int num, string orig) {
  26.     string ret = "";
  27.     for (int i = 0; i < orig.length();  i++)
  28.     {
  29.         // Only process if we have a character
  30.         if (isalpha(orig[i]))
  31.         {
  32.             // Shift taking into account capital letters
  33.             if (isalpha(orig[i] + (num % 26)))
  34.             {
  35.             ret += (orig[i] + (num % 26));
  36.             }
  37.             else
  38.             {
  39.             ret += (orig[i] + ((num % 26) - 26));
  40.             }
  41.         }
  42.         else
  43.         {
  44.             ret += (orig[i]);
  45.         }
  46.        
  47.     }
  48.     return ret;
  49. }
  50.  
  51. void printOutput(string output) {
  52.     cout << "Here it is:" << '\n';
  53.     cout << output << '\n';
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement