Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <limits>
- using namespace std;
- // prototypes
- string encode(int num, string orig);
- void printOutput(string output);
- int main(int argc, char *argv[])
- {
- cout << "Enter number" << '\n';
- int k = 0;
- cin >> k;
- cout << "Enter text" << '\n';
- // The number of times we are going to interate the characters in the argument of the program
- string text;
- // Get user input of the string we will encode
- cin.ignore (numeric_limits<streamsize>::max(), '\n');
- getline (cin,text);
- printOutput(encode(k,text));
- return 0;
- }
- string encode(int num, string orig) {
- string ret = "";
- for (int i = 0; i < orig.length(); i++)
- {
- // Only process if we have a character
- if (isalpha(orig[i]))
- {
- // Shift taking into account capital letters
- if (isalpha(orig[i] + (num % 26)))
- {
- ret += (orig[i] + (num % 26));
- }
- else
- {
- ret += (orig[i] + ((num % 26) - 26));
- }
- }
- else
- {
- ret += (orig[i]);
- }
- }
- return ret;
- }
- void printOutput(string output) {
- cout << "Here it is:" << '\n';
- cout << output << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement