Decrypt the String according to given algorithm | C ++ implementation of the approach ; Function that returns ( num % 26 ) ; Initialize result ; One by one process all digits of ' num ' ; Function to return the decrypted string ; To store the final decrypted answer ; One by one check for each character if it is a numeric character ; Modulo the number found in the string by 26 ; Driver code ; Print the decrypted string
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MOD = 26 ; int modulo_by_26 ( string num ) { int res = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) res = ( res * 10 + ( int ) num [ i ] - '0' ) % MOD ; return res ; } string decrypt_message ( string s ) { string decrypted_str = " " ; string num_found_so_far = " " ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s [ i ] >= '0' && s [ i ] <= '9' ) { num_found_so_far += s [ i ] ; } else if ( num_found_so_far . length ( ) > 0 ) { decrypted_str += ' a ' + modulo_by_26 ( num_found_so_far ) ; num_found_so_far = " " ; } } if ( num_found_so_far . length ( ) > 0 ) { decrypted_str += ' a ' + modulo_by_26 ( num_found_so_far ) ; } return decrypted_str ; } int main ( ) { string s = "32ytAAcV4ui30hf10hj18" ; cout << decrypt_message ( s ) ; return 0 ; }