Modify string by increasing each character by its distance from the end of the word | C ++ implementation of the above approach ; Function to transform and return the transformed word ; Stores resulting word ; Iterate over the word ; Add the position value to the letter ; Convert it back to character ; Add it to the string ; Function to transform the given string ; Size of string ; Stores resultant string ; Iterate over given string ; End of word is reached ; Append the word ; For the last word ; Driver code ; Given string ; Function call
#include <bits/stdc++.h> NEW_LINE 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 ; }