Open In App

Encode given string by shifting each character forward with its alphabetical value

Last Updated : 27 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given string str of size N consisting of lowercase English alphabets, the task is to encode the given string as follows:

  • change every character of that string to another character
  • the distance between the changed character and the current character is the same as the distance between the current character and 'a'.
  • Also, assume that the character's array forms a cycle, i.e. after 'z' the cycle starts again from 'a'.

Examples:

Input: str = "geeksforgeeks"
Output: "miiukkcimiiuk"
Explanation:
g changed to m (distance between g & a is 6, distance between m & g is 6)
e changed to i (distance between e & a is 4, distance between i & e is 4)
and same for other characters as well.

Input: str = "cycleofalphabet"
Output: "ewewickaweoacim"

 

Approach: This problem can be solved using the following steps:

  • Run a loop from i=0 to i<N and traverse each character of the string. For each character str[i]:
    • Find the distance between str[i] and 'a', i.e. dist=str[i]-'a'.
    • Now, if (dist+(str[i]-'a')) > 26, this means that 'z' is exceeded, so
  • Otherwise, change str[i] to str[i]+dist.
  • Print the string as the answer to this problem.

Below is the implementation of the above approach:

C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to change every character
// of the string to another character
void changeString(string str)
{
    for (auto& x : str) {
        int dist = x - 'a';

        // If 'z' is exceeded
        if (dist + (x - 'a') >= 26) {
            dist = (dist + (x - 'a')) % 26;
            x = 'a' + dist;
        }

        // If 'z' is not exceeded
        else {
            x = x + dist;
        }
    }
    cout << str << endl;
}

// Driver Code
int main()
{
    string str = "nayan";
    changeString(str);
    return 0;
}
Java
// Jsvs code for the above approach
import java.util.*;

class GFG {

  // Function to change every character
  // of the string to another character
  static void changeString(String str)
  {
    char[] ch = str.toCharArray();
    for (int i = 0; i < str.length(); i++) {
      int dist = ch[i] - 'a';

      // If 'z' is exceeded
      if (dist + (ch[i] - 'a') >= 26) {
        dist = (dist + (ch[i] - 'a')) % 26;
        ch[i] = (char)('a' + dist);
      }

      // If 'z' is not exceeded
      else {
        ch[i] = (char)(ch[i] + dist);
      }
    }

    String s = new String(ch);
    System.out.println(s);
  }

  // Driver Code
  public static void main(String[] args)
  {
    String str = "cycleofalphabet";
    changeString(str);
  }
}

// This code is contributed by ukasp.
Python3
# Python code for the above approach 

# Function to change every character
# of the string to another character
def changeString(str):
    str = list(str)
    for x in range(len(str)):
        dist = ord(str[x]) - ord('a')

        # If 'z' is exceeded
        if (dist + (ord(str[x]) - ord('a')) >= 26):
            dist = (dist + (ord(str[x]) - ord('a'))) % 26;
            str[x] = chr(ord('a') + dist);
        

        # If 'z' is not exceeded
        else:
            str[x] = chr(ord(str[x]) + dist);
        
    str = "".join(str)
    print(str)

# Driver Code

str = "cycleofalphabet";
changeString(str);

# This code is contributed by Saurabh Jaiswal
C#
// C# code for the above approach
using System;
using System.Collections;

class GFG
{

  // Function to change every character
  // of the string to another character
  static void changeString(string str)
  {
    char[] ch = str.ToCharArray();  
    for(int i = 0; i < str.Length; i++) {
      int dist = ch[i] - 'a';

      // If 'z' is exceeded
      if (dist + (ch[i] - 'a') >= 26) {
        dist = (dist + (ch[i] - 'a')) % 26;
        ch[i] = (char)('a' + dist);
      }

      // If 'z' is not exceeded
      else {
        ch[i] = (char)(ch[i] + dist);
      }
    }

    string s = new string(ch);
    Console.WriteLine(s);
  }

  // Driver Code
  public static void Main()
  {
    string str = "cycleofalphabet";
    changeString(str);
  }
}

// This code is contributed by Samim Hossain Mondal.
JavaScript
  <script>
        // JavaScript code for the above approach 

        // Function to change every character
        // of the string to another character
        function changeString(str) {
            str = str.split('')
            for (let x = 0; x < str.length; x++) {
                let dist = str[x].charCodeAt(0) - 'a'.charCodeAt(0);

                // If 'z' is exceeded
                if (dist + (str[x].charCodeAt(0) - 'a'.charCodeAt(0)) >= 26) {
                    dist = (dist + (str[x].charCodeAt(0) - 'a'.charCodeAt(0))) % 26;
                    str[x] = String.fromCharCode('a'.charCodeAt(0) + dist);
                }

                // If 'z' is not exceeded
                else {
                    str[x] = String.fromCharCode(str[x].charCodeAt(0) + dist);
                }
            }
            str = str.join('')
            document.write(str + "<br>")
        }

        // Driver Code

        let str = "cycleofalphabet";
        changeString(str);

       // This code is contributed by Potta Lokesh
    </script>

 
 


Output
ewewickaweoacim


 

Time Complexity: O(N)
Auxiliary Space: O(1)


 


Next Article
Practice Tags :

Similar Reads