Modify string by increasing each character by its distance from the end of the word | Java implementation of the above approach ; 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 ; 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 ; Driver Code ; Given string ; Function Call
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 ) ; } }