Find k 'th character of decrypted string | Set 1 | C ++ program to find K 'th character in decrypted string ; Function to find K 'th character in Encoded String ; expand string variable is used to store final string after decompressing string str ; string temp ; Current substring int freq = 0 ; Count of current substring ; read characters until you find a number or end of string ; push character in temp ; read number for how many times string temp will be repeated in decompressed string ; generating frequency of temp ; now append string temp into expand equal to its frequency ; this condition is to handle the case when string str is ended with alphabets not with numeric value ; Driver program to test the string
#include <bits/stdc++.h> NEW_LINE using namespace std ; char encodedChar ( string str , int k ) { string expand = " " ; for ( int i = 0 ; str [ i ] != ' \0' ; ) { while ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) { temp . push_back ( str [ i ] ) ; i ++ ; } while ( str [ i ] >= '1' && str [ i ] <= '9' ) { freq = freq * 10 + str [ i ] - '0' ; i ++ ; } for ( int j = 1 ; j <= freq ; j ++ ) expand . append ( temp ) ; } if ( freq == 0 ) expand . append ( temp ) ; return expand [ k - 1 ] ; } int main ( ) { string str = " ab4c12ed3" ; int k = 21 ; cout << encodedChar ( str , k ) << endl ; return 0 ; }