Modify string by increasing each character by its distance from the end of the word
Last Updated :
09 Dec, 2022
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:
- First, tokenize the given string S into individual words.
- Iterate over words and for each character in a word, add its position from the end to it.
- Then, add the resultant word to the final string, say res.
- Keep repeating the above two steps until every word in the string is transformed.
Below is the program for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string util(string sub)
{
int n = sub.length();
int i = 0;
string ret = "" ;
while (i < n)
{
int t = (sub[i] - 'a' ) +
n - 1 - i;
char ch = ( char )(t % 26 + 97);
ret = ret + ch;
i++;
}
return ret;
}
void manipulate(string s)
{
int n = s.length();
int i = 0, j = 0;
string res = "" ;
while (i < n)
{
if (s[i] == ' ' )
{
res += util(s.substr(j, i));
res = res + " " ;
j = i + 1;
i = j + 1;
}
else
{
i++;
}
}
res = res + util(s.substr(j, i));
cout << res << endl;
}
int main()
{
string s = "acm fkz" ;
manipulate(s);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void manipulate(String s)
{
int n = s.length();
int i = 0 , j = 0 ;
String res = "" ;
while (i < n) {
if (s.charAt(i) == ' ' ) {
res += util(s.substring(j, i));
res = res + " " ;
j = i + 1 ;
i = j + 1 ;
}
else {
i++;
}
}
res = res + util(s.substring(j, i));
System.out.println(res);
}
public static String util(String sub)
{
int n = sub.length();
int i = 0 ;
String ret = "" ;
while (i < n) {
int t = (sub.charAt(i) - 'a' )
+ n - 1 - i;
char ch = ( char )(t % 26 + 97 );
ret = ret + String.valueOf(ch);
i++;
}
return ret;
}
public static void main(String[] args)
{
String s = "acm fkz" ;
manipulate(s);
}
}
|
Python3
def util(sub):
n = len (sub)
i = 0
ret = ""
while i < n:
t = ( ord (sub[i]) - 97 ) + n - 1 - i
ch = chr (t % 26 + 97 )
ret = ret + ch
i = i + 1
return ret
def manipulate(s):
n = len (s)
i = 0
j = 0
res = ""
while i < n:
if s[i] = = ' ' :
res + = util(s[j : j + i])
res = res + " "
j = i + 1
i = j + 1
else :
i = i + 1
res = res + util(s[j : j + i])
print (res)
if __name__ = = "__main__" :
s = "acm fkz"
manipulate(s)
|
C#
using System;
class GFG{
public static void manipulate(String s)
{
int n = s.Length;
int i = 0, j = 0;
String res = "" ;
while (i < n)
{
if (s[i] == ' ' )
{
res += util(s.Substring(j, i - j));
res = res + " " ;
j = i + 1;
i = j + 1;
}
else
{
i++;
}
}
res = res + util(s.Substring(j, i - j));
Console.WriteLine(res);
}
public static String util(String sub)
{
int n = sub.Length;
int i = 0;
String ret = "" ;
while (i < n)
{
int t = (sub[i] - 'a' ) +
n - 1 - i;
char ch = ( char )(t % 26 + 97);
ret = ret + String.Join( "" , ch);
i++;
}
return ret;
}
public static void Main(String[] args)
{
String s = "acm fkz" ;
manipulate(s);
}
}
|
Javascript
function util(sub)
{
var n = sub.length;
var i = 0;
var ret = "" ;
while (i < n)
{
var t = (sub[i] - 'a' ) +
n - 1 - i;
var ch = (char)(t % 26 + 97);
ret = ret + ch;
i++;
}
return ret;
}
function manipulate(s)
{
var n = s.length();
var i = 0, j = 0;
var res = "" ;
while (i < n)
{
if (s[i] == ' ' )
{
res += util(s.substring(j, i));
res = res + " " ;
j = i + 1;
i = j + 1;
}
else
{
i++;
}
}
res = res + util(s.substring(j, i));
console.log( "res|n" );
}
var s = "acm fkz" ;
manipulate(s);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...