Replace every character by character that is N positions down the alphabet of the String

Given a string ‘s’ and a number ‘n’, write a function that returns a string with each character in s replaced with another character that is ‘n’ positions down the alphabet.
For example:

  • If n is 1, A is replaced with B, B is replaced with C and so on.
  • If n is 2, A is replaced with C, B is replaced with D and so on.
  • If n is 3, X is replaced with A, Y is replaced with B and so on.

Algorithm

  • Check passed value “n”. If it is greater than 26 then apply modulo function with 26. As after 26, replacement will always be repeated.
  • Iterate through each character in given string.
    • add the number “n” to the input character.
    • if new character ASCII value is greater than “Z” then subtract 26 from it and save it.
    • Save the string in output character array.

Let’s have a look into the sample code.

void string_encoder (const char* input, int n, char* output)
{
    int len = strlen (input);

    if (len == 0) {
        cout << "Empty String Received !!" << endl;
        return;
    }

    int mod_n = n % 26;
    for (int i = 0; i < len; i++) {
        output[i] = input[i] + mod_n;
        if (output[i] > 'Z') {
            output[i] = output[i] - 'Z' + 'A' - 1;
        }
    }
}

Define a function to convert back the encoded string to original string

Here we will define a function which will take the output string from the previous section and convert it back to original string.

Algorithm

  • Check passed value “n”. If it is greater than 26 then apply modulo function with 26. As after 26, replacement will always be repeated.
  • Iterate through each character in given string.
    • subtract the number “n” to the input character.
    • if new character ASCII value is lesser than “A” then add 26 to it and save it.
    • Save the string in output character array.

Let’s have a look into the sample code.

void string_decoder (const char* output, int n)
{
    int len = strlen (output);

    if (len == 0) {
        cout << "Empty String Received !!" << endl;
        return;
    }

    int mod_n = n % 26;
    for (int i =0; i < len; i++) {
        char c = output[i] - mod_n;
        if (c < 'A') {
            c = 'Z' - ('A' - c) + 1;
        }
        cout << c;
    }
    cout << endl;
}

Let’s look into a main function which utilizes both the functions.

int main ()
{
    char str[50] = {0};
    char output[50] = {0};
    int n = 0;
    cout << "Input String " << endl;
    cin >> str;
    cout << "Input value of n" << endl;
    cin >> n;

    cout << "Input  String: " << str << endl;
    string_encoder (&str[0], n, &output[0]);
    cout << "Encoded String: " << output << endl;
    cout << "Decoded String: " ;
    string_decoder (output, n);
}

Let’s analyze the output of above main function.

Input String 
ACF
Input value of n
1
Input  String: ACF
Encoded String: BDG
Decoded String: ACF
----------------------------------------------------------
Input String 
AXZ
Input value of n
1
Input  String: AXZ
Encoded String: BYA
Decoded String: AXZ
----------------------------------------------------------
Input String 
AGI
Input value of n
26
Input  String: AGI
Encoded String: AGI
Decoded String: AGI
----------------------------------------------------------
Input String 
AXY
Input value of n
27
Input  String: AXY
Encoded String: BYZ
Decoded String: AXY

Leave a Reply

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