# Caesar Concatenation

• Last Updated : 03 Jan, 2022

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 ``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`

Output:

`KiiojsvKiiowKeikw163`

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

My Personal Notes arrow_drop_up