text: Replace every character of string by character whose ASCII value is K times more than it | CPP 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
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void encode ( string s , int k ) { string newS ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { int val = int ( s [ 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 ; } cout << newS ; } int main ( ) { string str = " abc " ; int k = 28 ; encode ( str , k ) ; return 0 ; }