Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Your task is to add up letters to one letter.
- The function will be given a variable amount of arguments, each one being a letter to add.
- Notes:
- Letters will always be lowercase.
- Letters can overflow (see second to last example of the description)
- If no letters are given, the function should return 'z'
- Examples:
- add_letters('a', 'b', 'c') = 'f'
- add_letters('a', 'b') = 'c'
- add_letters('z') = 'z'
- add_letters('z', 'a') = 'a'
- add_letters('y', 'c', 'b') = 'd' # notice the letters overflowing
- add_letters() = 'z'
- */
- #include <iostream>
- #include <map>
- #include <algorithm>
- using namespace std;
- // Auxiliary Functions
- // **************************************************************************************************************************
- // **************************************************************************************************************************
- // a) Converts a letter (such as 'd') ---> to an integer from 1 to 26 (such as 4)
- int charToInt(char ch){
- map <char, int> myMap = map <char, int> ();
- int counter = 0;
- for(char i='a'; i<='z'; i++){
- counter++;
- myMap[i] = counter;
- }
- // Iterator creation
- map <char, int> :: iterator itr;
- for(itr=myMap.begin(); itr!=myMap.end(); itr++){
- if(itr->first == ch){
- return itr->second;
- }
- }
- }
- // b) Converts an integer (such as 4) ---> to a char (such as 'd')
- char intToChar(int n){
- map <int, char> myMap = map <int, char> ();
- int counter = 0;
- for(char i='a'; i<='z'; i++){
- counter++;
- myMap[counter] = i;
- }
- // Iterator creation
- map <int, char> :: iterator itr;
- for(itr=myMap.begin(); itr!=myMap.end(); itr++){
- if(itr->first == n){
- return itr->second;
- }
- }
- }
- // **************************************************************************************************************************
- // **************************************************************************************************************************
- // 1. My Functions with no parameters
- char add_letters(){
- return 'z';
- }
- // 2. My function with 1 parameter
- char add_letters(char ch){
- return ch;
- }
- // 3. My function with 2 parameters
- char add_letters(char ch1, char ch2){
- int num1 = charToInt(ch1);
- int num2 = charToInt(ch2);
- int num = (num1 + num2) % 26;
- return intToChar(num);
- }
- // 4. My function with 3 parameters
- char add_letters(char ch1, char ch2, char ch3){
- char ch4 = add_letters(ch1, ch2);
- return add_letters(ch3, ch4);
- }
- // **************************************************************************************************************************
- // **************************************************************************************************************************
- // Main Function
- int main()
- {
- cout << "1. No parameters: add_letters() ---> " << "'" << add_letters() << "'" << endl;
- cout << "2. With 1 parameter: add_letters('e') ---> " << "'" << add_letters('e') << "'" << endl;
- cout << "3a. With 2 parameters: add_letters('b','f') ---> " << "'" << add_letters('b', 'f') << "'" << endl;
- cout << "3b. With 2 parameters: add_letters('x','d') ---> " << "'" << add_letters('x', 'd') << "'" << endl;
- cout << "4a. With 3 parameters: add_letters('a','b', 'c') ---> " << "'" << add_letters('a', 'b', 'c') << "'" << endl;
- cout << "4b. With 3 parameters: add_letters('b','d','y') ---> " << "'" << add_letters('b', 'd', 'y') << "'" << endl;
- cout << "4c. With 3 parameters: add_letters('x','y','z') ---> " << "'" << add_letters('x', 'y', 'z') << "'" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement