Caesar Concatenation
Tags: algorithms, string, cpp, cryptography
Published: 2021-02-13 12:33:11

Given two strings str1 and str2 containing alphanumeric characters and a number N. The task is to form a new encrypted string which contains the string str1 with a Ceaser Encryption of N characters and the string str2 with a Ceaser 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:

**Case 1**: If characters lies between ‘A’ and ‘Z’ then the current character is encrypted as:
```
new_character = ( (current_character - 65 + N) % 26 ) + 65;
```

**Case 2**: If characters lies between ‘a’ and ‘z’ then the current character is encrypted as:
```
new_character = ( (current_character - 97 + N) % 26 ) + 97;
```

**Case 3**: If characters lies between ‘A’ and ‘Z’ then the current character is encrypted as:
```
new_character = ( (current_character - 48 + N) % 10 ) + 48;
```

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; 
} 
```
Output:
```
KiiojsvKiiowKeikw163
```

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