Modify a string by circularly shifting each character to the right by respective frequencies | C ++ program for the above approach ; Function to replace the characters by its frequency of character in it ; Stores frequencies of characters in the string S ; Traverse the string S ; Increment the frequency of each character by 1 ; Traverse the string S ; Find the frequency of the current character ; Update the character ; Print the resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void addFrequencyToCharacter ( string S ) { int frequency [ 26 ] = { 0 } ; int N = S . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { frequency [ S [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < N ; i ++ ) { int add = frequency [ S [ i ] - ' a ' ] % 26 ; if ( int ( S [ i ] ) + add <= int ( ' z ' ) ) S [ i ] = char ( int ( S [ i ] ) + add ) ; else { add = ( int ( S [ i ] ) + add ) - ( int ( ' z ' ) ) ; S [ i ] = char ( int ( ' a ' ) + add - 1 ) ; } } cout << S ; } int main ( ) { string S = " geeksforgeeks " ; addFrequencyToCharacter ( S ) ; return 0 ; }