Encode given string by shifting each character forward with its alphabetical value
Last Updated :
27 Jun, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void changeString(string str)
{
for ( auto & x : str) {
int dist = x - 'a' ;
if (dist + (x - 'a' ) >= 26) {
dist = (dist + (x - 'a' )) % 26;
x = 'a' + dist;
}
else {
x = x + dist;
}
}
cout << str << endl;
}
int main()
{
string str = "nayan" ;
changeString(str);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void changeString(String str)
{
char [] ch = str.toCharArray();
for ( int i = 0 ; i < str.length(); i++) {
int dist = ch[i] - 'a' ;
if (dist + (ch[i] - 'a' ) >= 26 ) {
dist = (dist + (ch[i] - 'a' )) % 26 ;
ch[i] = ( char )( 'a' + dist);
}
else {
ch[i] = ( char )(ch[i] + dist);
}
}
String s = new String(ch);
System.out.println(s);
}
public static void main(String[] args)
{
String str = "cycleofalphabet" ;
changeString(str);
}
}
|
Python3
def changeString( str ):
str = list ( str )
for x in range ( len ( str )):
dist = ord ( str [x]) - ord ( 'a' )
if (dist + ( ord ( str [x]) - ord ( 'a' )) > = 26 ):
dist = (dist + ( ord ( str [x]) - ord ( 'a' ))) % 26 ;
str [x] = chr ( ord ( 'a' ) + dist);
else :
str [x] = chr ( ord ( str [x]) + dist);
str = "".join( str )
print ( str )
str = "cycleofalphabet" ;
changeString( str );
|
C#
using System;
using System.Collections;
class GFG
{
static void changeString( string str)
{
char [] ch = str.ToCharArray();
for ( int i = 0; i < str.Length; i++) {
int dist = ch[i] - 'a' ;
if (dist + (ch[i] - 'a' ) >= 26) {
dist = (dist + (ch[i] - 'a' )) % 26;
ch[i] = ( char )( 'a' + dist);
}
else {
ch[i] = ( char )(ch[i] + dist);
}
}
string s = new string (ch);
Console.WriteLine(s);
}
public static void Main()
{
string str = "cycleofalphabet" ;
changeString(str);
}
}
|
Javascript
<script>
function changeString(str) {
str = str.split( '' )
for (let x = 0; x < str.length; x++) {
let dist = str[x].charCodeAt(0) - 'a' .charCodeAt(0);
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);
}
else {
str[x] = String.fromCharCode(str[x].charCodeAt(0) + dist);
}
}
str = str.join( '' )
document.write(str + "<br>" )
}
let str = "cycleofalphabet" ;
changeString(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...