Skip to content

techtrendings.com

Let's explore

Menu
Menu

Caesar Cipher Problem C++ HackerRank

Posted on January 18, 2022January 18, 2022 by Avidlearner

Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar’s cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.

Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +3: defghijklmnopqrstuvwxyzabc

Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.

Sample Input

11
middle-Outz
2
Sample Output

okffng-Qwvb
Explanation

Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +2: cdefghijklmnopqrstuvwxyzab

  • Complete the ‘caesarCipher’ function below.
    *
  • The function is expected to return a STRING.
  • The function accepts following parameters:
  • 1. STRING s
  • 2. INTEGER k
    */
string caesarCipher(string s, int k) {
    string str;
    for(int i=0;i<s.length();i++)
    {
        k=k%26;
        for(char ch='a';ch <= 'z';ch++) //97-122
        {
            if(s[i] == ch )
            {
                
                if(s[i] + k > 122 )   /
                {
                    s[i]= s[i] + k - 26;
                    
                }
                else {
                     s[i] = s[i] + k;
                    
                }
                break;
            }
            else if(s[i] == ch-32) 
            {
               
               if(s[i] + k > 90 )   
                {
                    s[i]= s[i] + k - 26; 
                }
                else {
                     
                     s[i] = s[i] + k ; 
                }
                break; 
            }
        }
        
    }
    s[s.length()]='\0';
    return s;
}
int main()
{
    string n_temp;
    getline(cin, n_temp);

    int n = stoi(ltrim(rtrim(n_temp)));

    string s;
    getline(cin, s);

    string k_temp;
    getline(cin, k_temp);

    int k = stoi(ltrim(rtrim(k_temp)));

    string result = caesarCipher(s, k);

    fout << result << "\n";

    fout.close();

    return 0;
}

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Implement Trie Data Structure in C++- LeetCode
  • How TLS Works
  • C++ – Factory Design Pattern – Creation Design Pattern
  • C++ – Strategy Design Pattern – Behavioral Design Pattern
  • LFU Cache Implementation – LeetCode

Recent Comments

  • automatically like friends photos on instagram on Program to find unpaired element in an Array in C++|Leetcode |techtrendings
  • Twicsy on Program to find unpaired element in an Array in C++|Leetcode |techtrendings

Archives

  • January 2023
  • November 2022
  • August 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022

Categories

  • Algorithm
  • Algorithm
  • C++
  • Design Patterns
  • Multithreading
  • OS Concepts
  • Programming
  • Uncategorized

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Join Our Mailing List for the Latest News and Updates.

© 2023 techtrendings.com | Powered by Superbs Personal Blog theme