Open In App

Caesar Concatenation

Last Updated : 22 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two strings str1 and str2 containing alpha-numeric characters and a number N. The task is to form a new encrypted string which contains the string str1 with a Caesar Encryption of N characters and the string str2 with a Caesar Encryption of N characters at odd indices.
Example: 
 

Input: str1 = “GeekforGeeks”, str2 = “Geeks123”, N = 4 
Output: KiiojsvKiiowKeikw163 
Explanation: 
Caesar Text for string str1 with a shift of 4 is “KiiojsvKiiow” 
Caesar Text for string str2 with a shift of 4 at all even indexes is “Keikw163” 
Resultant string is “KiiojsvKiiow” + “Keikw163” = “KiiojsvKiiowKeikw163”
Input: str1 = “ABcdE23”, str2 = “efda2w”, N = 9 
Output: JKlmN12nfma1w 
Explanation: 
Caesar Text for string str1 with a shift of 9 is “JKlmN12” 
Caesar Text for string str2 with a shift of 9 at all even indexes is “nfma1w” 
Resultant string is “JKlmN12” + “nfma1w” = “JKlmN12nfma1w” 
 

 

Approach: 
This problem is an application of Caesar Cipher in Cryptography. Below are the steps: 
The idea is to traverse the given string str1 and str2 and convert all the characters at every index of str1 and at even indexes of str2 by a shift of N on the basis of below 3 cases: 
 

  1. Case 1: If characters lies between ‘A’ and ‘Z’ then the current character is encrypted as: 
     
new_character = ( (current_character - 65 + N) % 26 ) + 65;
  1.  
  2. Case 2: If characters lies between ‘a’ and ‘z’ then the current character is encrypted as: 
     
new_character = ( (current_character - 97 + N) % 26 ) + 97;
  1.  
  2. Case 3: If characters lies between ‘A’ and ‘Z’ then the current character is encrypted as: 
     
new_character = ( (current_character - 48 + N) % 10 ) + 48;
  1.  

Below is the implementation of the above approach:
 

CPP




// C++ implementation of the above
// approach
#include <bits/stdc++.h>
using namespace std;
 
void printCaesarText(string str1,
                    string str2, int N)
{
 
    // Traverse the string str1
    for (int i = 0; str1[i]; i++) {
 
        // Current character
        char ch = str1[i];
 
        // Case 1:
        if (ch >= 'A' && ch <= 'Z') {
            str1[i] = (ch - 65 + N) % 26 + 65;
        }
 
        // Case 2:
        else if (ch >= 'a' && ch <= 'z') {
            str1[i] = (ch - 97 + N) % 26 + 97;
        }
 
        // Case 3:
        else if (ch >= '0' && ch <= '9') {
            str1[i] = (ch - 48 + N) % 10 + 48;
        }
    }
 
    for (int i = 0; str2[i]; i++) {
 
        // If current index is odd, then
        // do nothing
        if (i & 1)
            continue;
 
        // Current character
        char ch = str2[i];
 
        // Case 1:
        if (ch >= 'A' && ch <= 'Z') {
            str2[i] = (ch - 65 + N) % 26 + 65;
        }
 
        // Case 2:
        else if (ch >= 'a' && ch <= 'z') {
            str2[i] = (ch - 97 + N) % 26 + 97;
        }
 
        // Case 3:
        else if (ch >= '0' && ch <= '9') {
            str2[i] = (ch - 48 + N) % 10 + 48;
        }
    }
 
    // Print the concatenated strings
    // str1 + str2
    cout << str1 + str2;
}
 
// Driver Code
int main()
{
 
    string str1 = "GeekforGeeks";
    string str2 = "Geeks123";
    int N = 4;
 
    printCaesarText(str1, str2, N);
 
    return 0;
}


Python3




# Python implementation of the above
# approach
def printCaesarText(str1, str2, N):
     
    # Traverse the string str1
    for i in range(len(str1)):
         
        # Current character
        ch = str1[i]
         
        # Case 1:
        if (ch >= 'A' and ch <= 'Z'):
            str1[i] = chr((ord(ch) - 65 + N) % 26 + 65)
             
        # Case 2:
        elif (ch >= 'a' and ch <= 'z'):
            str1[i] = chr((ord(ch) - 97 + N) % 26 + 97)
         
        # Case 3:
        elif (ch >= '0' and ch <= '9'):
            str1[i] = chr((ord(ch) - 48 + N) % 10 + 48)
             
    for i in range(len(str2)):
         
        # If current index is odd, then
        # do nothing
        if (i & 1):
            continue
         
        # Current character
        ch = str2[i]
         
        # Case 1:
        if (ch >= 'A' and ch <= 'Z'):
            str2[i] = chr((ord(ch) - 65 + N) % 26 + 65)
             
        # Case 2:
        elif (ch >= 'a' and ch <= 'z'):
            str2[i] = chr((ord(ch) - 97 + N) % 26 + 97)
         
        # Case 3:
        elif (ch >= '0' and ch <= '9'):
            str2[i] = chr((ord(ch) - 48 + N) % 10 + 48)
     
    # Print the concatenated strings
    # str1 + str2
    print("".join(str1 + str2))
 
# Driver Code
str1 = "GeekforGeeks"
str2 = "Geeks123"
N = 4
 
printCaesarText(list(str1), list(str2), N)
 
# This code is contributed by Shubham Singh


Javascript




// javascript implementation of the above
// approach
 
function printCaesarText(str1, str2, N)
{
  
    // Traverse the string str1
    for (let i = 0; i < str1.length; i++) {
  
        // Current character
        let ch = str1[i];
  
        // Case 1:
        if (ch >= 'A' && ch <= 'Z') {
            str1[i] = String.fromCharCode((ch.charCodeAt(0) - 65 + N) % 26 + 65);
        }
  
        // Case 2:
        else if (ch >= 'a' && ch <= 'z') {
            str1[i] = String.fromCharCode((ch.charCodeAt(0) - 97 + N) % 26 + 97);
        }
  
        // Case 3:
        else if (ch >= '0' && ch <= '9') {
            str1[i] = String.fromCharCode((ch.charCodeAt(0) - 48 + N) % 10 + 48);
        }
    }
  
    for (let i = 0; i < str2.length;i++) {
  
        // If current index is odd, then
        // do nothing
        if (i%2 != 0)
            continue;
  
        // Current character
        let ch = str2[i];
  
        // Case 1:
        if (ch >= 'A' && ch <= 'Z') {
            str2[i] = String.fromCharCode((ch.charCodeAt(0) - 65 + N) % 26 + 65);
        }
  
        // Case 2:
        else if (ch >= 'a' && ch <= 'z') {
            str2[i] = String.fromCharCode((ch.charCodeAt(0) - 97 + N) % 26 + 97);
        }
  
        // Case 3:
        else if (ch >= '0' && ch <= '9') {
            str2[i] = String.fromCharCode((ch.charCodeAt(0) - 48 + N) % 10 + 48);
        }
    }
  
    // Print the concatenated strings
    // str1 + str2
    console.log(str1.join("") + str2.join(""));
}
  
// Driver Code
let str1 = "GeekforGeeks";
let str2 = "Geeks123";
let N = 4;
 
printCaesarText(str1.split(""), str2.split(""), N);
 
// The code is contributed by Nidhi goel.


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    
// Java implementation of the above approach
public static void printCaesarText(String str1, String str2, int N)
{
    // Traverse the string str1
    for (int i = 0; i < str1.length(); i++)
    {
        // Current character
        char ch = str1.charAt(i);
   
        // Case 1:
        if (ch >= 'A' && ch <= 'Z')
        {
            str1 = str1.substring(0, i) + (char)((int)(ch - 65 + N) % 26 + 65) + str1.substring(i + 1);
        }
   
        // Case 2:
        else if (ch >= 'a' && ch <= 'z')
        {
            str1 = str1.substring(0, i) + (char)((int)(ch - 97 + N) % 26 + 97) + str1.substring(i + 1);
        }
   
        // Case 3:
        else if (ch >= '0' && ch <= '9')
        {
            str1 = str1.substring(0, i) + (char)((int)(ch - 48 + N) % 10 + 48) + str1.substring(i + 1);
        }
    }
   
    // Traverse the string str2
    for (int i = 0; i < str2.length(); i++)
    {
        // If current index is odd, then
        // do nothing
        if (i % 2 == 1)
            continue;
   
        // Current character
        char ch = str2.charAt(i);
   
        // Case 1:
        if (ch >= 'A' && ch <= 'Z')
        {
            str2 = str2.substring(0, i) + (char)((int)(ch - 65 + N) % 26 + 65) + str2.substring(i + 1);
        }
   
        // Case 2:
        else if (ch >= 'a' && ch <= 'z')
        {
            str2 = str2.substring(0, i) + (char)((int)(ch - 97 + N) % 26 + 97) + str2.substring(i + 1);
        }
   
        // Case 3:
        else if (ch >= '0' && ch <= '9')
        {
            str2 = str2.substring(0, i) + (char)((int)(ch - 48 + N) % 10 + 48) + str2.substring(i + 1);
        }
    }
   
    // Print the concatenated strings
    // str1 + str2
    System.out.print(str1 + str2);
}
   
// Driver Code
public static void main(String args[])
{
    String str1 = "GeekforGeeks";
    String str2 = "Geeks123";
    int N = 4;
   
    printCaesarText(str1, str2, N);
}
}


C#




using System;
 
class Program {
    static void printCaesarText(string str1, string str2, int N) {
        // Traverse the string str1
        for (int i = 0; i < str1.Length; i++) {
            // Current character
            char ch = str1[i];
            // Case 1:
            if (ch >= 'A' && ch <= 'Z') {
                str1 = str1.Remove(i, 1).Insert(i, ((char)((ch - 65 + N) % 26 + 65)).ToString());
            }
            // Case 2:
            else if (ch >= 'a' && ch <= 'z') {
                str1 = str1.Remove(i, 1).Insert(i, ((char)((ch - 97 + N) % 26 + 97)).ToString());
            }
            // Case 3:
            else if (ch >= '0' && ch <= '9') {
                str1 = str1.Remove(i, 1).Insert(i, ((char)((ch - 48 + N) % 10 + 48)).ToString());
            }
        }
        for (int i = 0; i < str2.Length; i++) {
            // If current index is odd, then do nothing
            if ((i & 1) == 1) {
                continue;
            }
            // Current character
            char ch = str2[i];
            // Case 1:
            if (ch >= 'A' && ch <= 'Z') {
                str2 = str2.Remove(i, 1).Insert(i, ((char)((ch - 65 + N) % 26 + 65)).ToString());
            }
            // Case 2:
            else if (ch >= 'a' && ch <= 'z') {
                str2 = str2.Remove(i, 1).Insert(i, ((char)((ch - 97 + N) % 26 + 97)).ToString());
            }
            // Case 3:
            else if (ch >= '0' && ch <= '9') {
                str2 = str2.Remove(i, 1).Insert(i, ((char)((ch - 48 + N) % 10 + 48)).ToString());
            }
        }
        // Print the concatenated strings str1 + str2
        Console.WriteLine(str1 + str2);
    }
 
    static void Main(string[] args) {
        string str1 = "GeekforGeeks";
        string str2 = "Geeks123";
        int N = 4;
        printCaesarText(str1, str2, N);
    }
}


Output: 

KiiojsvKiiowKeikw163

 

Time Complexity: O(N + M), where N and M are the length of the two given string.
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads