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