Skip to content
Related Articles

Related Articles

Replace every character of string by character whose ASCII value is K times more than it

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 12 Sep, 2022
Improve Article
Save Article

Given string str consisting of lowercase letters only and an integer k, the task is to replace every character of the given string with a character whose ASCII value is k times more than it. If the ASCII value exceeds ‘z’, then start checking from ‘a’ in a cyclic manner.

Examples:  

Input: str = “abc”, k = 2 
Output: cde 
Explanation: 
a is moved by 2 times which results in character c 
b is moved by 2 times which results in character d 
c is moved by 2 times which results in character e

Input: str = “abc”, k = 28 
Output: cde 
Explanation: 
a is moved 25 times, z is reached. Then 26th character will be a, 27-th b and 28-th c. 
b is moved 24 times, z is reached. 28-th is d. 
b is moved 23 times, z is reached. 28-th is e. 

Approach: Iterate for every character in the string and perform the below steps for each character:  

  • Add k to the ASCII value of character str[i].
  • If it exceeds 122, then perform a modulus operation of k with 26 to reduce the number of steps, as 26 is the maximum number of shifts that can be performed in a rotation.
  • To find the character, add k to 96. Hence, the character with ASCII value k+96 will be a new character.

Repeat the above steps for every character of the given string.

Below is the implementation of the above approach: 

C++




// CPP program to move every character
// K times ahead in a given string
#include <bits/stdc++.h>
using namespace std;
 
// Function to move string character
void encode(string s,int k){
 
    // changed string
    string newS;
 
    // iterate for every characters
    for(int i=0; i<s.length(); ++i)
    {
        // ASCII value
        int val = int(s[i]);
 
        // store the duplicate
        int dup = k;
 
        // if k-th ahead character exceed 'z'
        if(val + k > 122){
            k -= (122-val);
            k = k % 26;
            newS += char(96 + k);
        }
        else
            newS += char(val + k);
 
        k = dup;
    }
 
    // print the new string
    cout<<newS;
}
 
// driver code
int main(){
    string str = "abc";
    int k = 28;
 
    // function call
    encode(str, k);
 
    return 0;
}
 
// This code is contributed by Sanjit_Prasad

Java




// Java program to move every character
// K times ahead in a given string
 
class GFG {
 
// Function to move string character
    static void encode(String s, int k) {
 
        // changed string
        String newS = "";
 
        // iterate for every characters
        for (int i = 0; i < s.length(); ++i) {
            // ASCII value
            int val = s.charAt(i);
            // store the duplicate
            int dup = k;
 
            // if k-th ahead character exceed 'z'
            if (val + k > 122) {
                k -= (122 - val);
                k = k % 26;
                 
                newS += (char)(96 + k);
            } else {
                newS += (char)(val + k);
            }
 
            k = dup;
        }
 
        // print the new string
        System.out.println(newS);
    }
 
// Driver Code
    public static void main(String[] args) {
        String str = "abc";
        int k = 28;
 
        // function call
        encode(str, k);
    }
}
 
// This code is contributed by Rajput-JI

Python3




# Python program to move every character
# K times ahead in a given string
 
# Function to move string character
def encode(s, k):
     
    # changed string
    newS = ""
     
    # iterate for every characters
    for i in range(len(s)):
         
        # ASCII value
        val = ord(s[i])
         
        # store the duplicate
        dup = k
         
        # if k-th ahead character exceed 'z'
        if val + k>122:
            k -= (122-val)
            k = k % 26
            newS += chr(96 + k)
             
        else:
            newS += chr(val + k)
         
        k = dup
     
    # print the new string
    print (newS)
             
# driver code    
str = "abc"
k = 28
 
encode(str, k)

C#




// C# program to move every character
// K times ahead in a given string
using System;
public class GFG {
 
// Function to move string character
    static void encode(String s, int k) {
 
        // changed string
        String newS = "";
 
        // iterate for every characters
        for (int i = 0; i < s.Length; ++i) {
            // ASCII value
            int val = s[i];
            // store the duplicate
            int dup = k;
 
            // if k-th ahead character exceed 'z'
            if (val + k > 122) {
                k -= (122 - val);
                k = k % 26;
                 
                newS += (char)(96 + k);
            } else {
                newS += (char)(96 + k);
            }
 
            k = dup;
        }
 
        // print the new string
        Console.Write(newS);
    }
 
// Driver Code
    public static void Main() {
        String str = "abc";
        int k = 28;
 
        // function call
        encode(str, k);
    }
}
 
// This code is contributed by Rajput-JI

PHP




<?php
// PHP program to move every character
// K times ahead in a given string
 
// Function to move string character
function encode($s, $k)
{
 
    // changed string
    $newS = "";
 
    // iterate for every characters
    for($i = 0; $i < strlen($s); ++$i)
    {
        // ASCII value
        $val = ord($s[$i]);
 
        // store the duplicate
        $dup = $k;
 
        // if k-th ahead character exceed 'z'
        if($val + $k > 122)
        {
            $k -= (122 - $val);
            $k = $k % 26;
            $newS = $newS.chr(96 + $k);
        }
        else
            $newS = $newS.chr($val + $k);
 
        $k = $dup;
    }
 
    // print the new string
    echo $newS;
}
 
// Driver code
 
$str = "abc";
$k = 28;
 
// function call
encode($str, $k);
 
// This code is contributed by ita_c
?>

Javascript




<script>
// Javascript program to move every character
// K times ahead in a given string
 
     
// Function to move string character
 
    function encode(s,k)
    {
        // changed string
        let newS = "";
   
        // iterate for every characters
        for (let i = 0; i < s.length; ++i) {
            // ASCII value
            let val = s[i].charCodeAt(0);
            // store the duplicate
            let dup = k;
   
            // if k-th ahead character exceed 'z'
            if (val + k > 122) {
                k -= (122 - val);
                k = k % 26;
                   
                newS += String.fromCharCode(96 + k);
            } else {
                newS += String.fromCharCode(val + k);
            }
   
            k = dup;
        }
   
        // print the new string
        document.write(newS);
    }
     
    // Driver Code
    let str = "abc";
    let k = 28;
     
    // function call
    encode(str, k);
     
 
 
// This code is contributed by rag2127
</script>

Output

cde

Complexity Analysis:

  • Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time, where N is the length of the string.
  • Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!