Modify string by increasing each character by its distance from the end of the word

Given a string S, the task is to modify the given string by replacing every character S[i] by a new character whose value is (S[i] + its position from the end of the word)

Examples:

Input: S = “acm fkz” 
Output: “cdm hlz” 
Explanation: 
There are 2 words in the given string {“acm”, “fkz”} 
For “acm”: 
a becomes ‘a’ + 2 = ‘c’ 
c becomes ‘c’ + 1 = ‘d’ 
m becomes ‘m’ + 0 = ‘m’ 
“acm” becomes “cdm”. 
Similarly, “fkz” becomes “hlz”. 
Therefore, the required answer is “cdm hlz”

Input: “geeks for geeks” 
Output: “khgls hpr khgls” 

Approach: The idea is to split the given string into words and modify each word individually. Below are the steps: 



  1. First, tokenize the given string S into individual words.
  2. Iterate over words and for each character in a word, add its position from the end to it.
  3. Then, add the resultant word to the final string, say res.
  4. Keep repeating the above two steps until every word in the string is transformed.

Below is the program for the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to transform and return
// the transformed word
string util(string sub)
{
    int n = sub.length();
    int i = 0;
 
    // Stores resulting word
    string ret = "";
 
    // Iterate over the word
    while (i < n)
    {
         
        // Add the position
        // value to the letter
        int t = (sub[i] - 'a') +
                  n - 1 - i;
 
        // Convert it back to character
        char ch = (char)(t % 26 + 97);
 
        // Add it to the string
        ret = ret + ch;
 
        i++;
    }
    return ret;
}
 
// Function to transform the
// given string
void manipulate(string s)
{
     
    // Size of string
    int n = s.length();
    int i = 0, j = 0;
 
    // Stores resultant string
    string res = "";
 
    // Iterate over given string
    while (i < n)
    {
 
        // End of word is reached
        if (s[i] == ' ')
        {
 
            // Append the word
            res += util(s.substr(j, i));
            res = res + " ";
            j = i + 1;
            i = j + 1;
        }
        else
        {
            i++;
        }
    }
 
    // For the last word
    res = res + util(s.substr(j, i));
 
    cout << res << endl;
}
 
// Driver code
int main()
{
     
    // Given string
    string s = "acm fkz";
 
    // Function call
    manipulate(s);
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of
// the above approach
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
 
    // Function to transform the given string
    public static void manipulate(String s)
    {
        // Size of string
        int n = s.length();
        int i = 0, j = 0;
 
        // Stores resultant string
        String res = "";
 
        // Iterate over given string
        while (i < n) {
 
            // End of word is reached
            if (s.charAt(i) == ' ') {
 
                // Append the word
                res += util(s.substring(j, i));
                res = res + " ";
                j = i + 1;
                i = j + 1;
            }
 
            else {
                i++;
            }
        }
 
        // For the last word
        res = res + util(s.substring(j, i));
 
        System.out.println(res);
    }
 
    // Function to transform and return
    // the transformed word
    public static String util(String sub)
    {
        int n = sub.length();
        int i = 0;
 
        // Stores resulting word
        String ret = "";
 
        // Iterate over the word
        while (i < n) {
 
            // Add the position
            // value to the letter
            int t = (sub.charAt(i) - 'a')
                    + n - 1 - i;
 
            // Convert it back to character
            char ch = (char)(t % 26 + 97);
 
            // Add it to the string
            ret = ret + String.valueOf(ch);
 
            i++;
        }
 
        return ret;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given string
        String s = "acm fkz";
 
        // Function Call
        manipulate(s);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of
# the above approach
 
# Function to transform and return
# the transformed word
def util(sub):
     
    n = len(sub)
    i = 0
 
    # Stores resulting word
    ret = ""
 
    # Iterate over the word
    while i < n:
 
        # Add the position
        # value to the letter
        t = (ord(sub[i]) - 97) + n - 1 - i
 
        # Convert it back to character
        ch = chr(t % 26 + 97)
 
        # Add it to the string
        ret = ret + ch
 
        i = i + 1
 
    return ret
 
# Function to transform the
# given string
def manipulate(s):
 
    # Size of string
    n = len(s)
    i = 0
    j = 0
 
    # Stores resultant string
    res = ""
 
    # Iterate over given string
    while i < n:
 
        # End of word is reached
        if s[i] == ' ':
 
            # print(s[j:j+i])
            # Append the word
            res += util(s[j : j + i])
            res = res + " "
            j = i + 1
            i = j + 1
        else:
            i = i + 1
 
    # For the last word
    res = res + util(s[j : j + i])
 
    print(res)
 
# Driver code
if __name__ == "__main__":
     
    # Given string
    s = "acm fkz"
 
    # Function call
    manipulate(s)
 
# This code is contributed by akhilsaini

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of
// the above approach
using System;
class GFG{
 
// Function to transform the given string
public static void manipulate(String s)
{
  // Size of string
  int n = s.Length;
  int i = 0, j = 0;
 
  // Stores resultant string
  String res = "";
 
  // Iterate over given string
  while (i < n)
  {
    // End of word is reached
    if (s[i] == ' ')
    {
      // Append the word
      res += util(s.Substring(j, i - j));
      res = res + " ";
      j = i + 1;
      i = j + 1;
    }
    else
    {
      i++;
    }
  }
 
  // For the last word
  res = res + util(s.Substring(j, i - j));
 
  Console.WriteLine(res);
}
 
// Function to transform and return
// the transformed word
public static String util(String sub)
{
  int n = sub.Length;
  int i = 0;
 
  // Stores resulting word
  String ret = "";
 
  // Iterate over the word
  while (i < n)
  {
    // Add the position
    // value to the letter
    int t = (sub[i] - 'a') +
             n - 1 - i;
 
    // Convert it back to character
    char ch = (char)(t % 26 + 97);
 
    // Add it to the string
    ret = ret + String.Join("", ch);
 
    i++;
  }
 
  return ret;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given string
  String s = "acm fkz";
 
  // Function Call
  manipulate(s);
}
}
 
// This code is contributed by shikhasingrajput

chevron_right


Output: 

cdm hlz



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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.