Decrypt a string according to given rules | C ++ program to decrypt the original string ; Function to return the original string after decryption ; Stores the decrypted string ; If length is odd ; Step counter ; Starting and ending index ; Iterate till all characters are decrypted ; Even step ; Odd step ; If length is even ; Step counter ; Starting and ending index ; Even step ; Odd step ; Reverse the decrypted string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decrypt ( string s , int l ) { string ans = " " ; if ( l % 2 ) { int cnt = 0 ; int indl = 0 , indr = l - 1 ; while ( ans . size ( ) != l ) { if ( cnt % 2 == 0 ) ans += s [ indl ++ ] ; else ans += s [ indr -- ] ; cnt ++ ; } } else { int cnt = 0 ; int indl = 0 , indr = l - 1 ; while ( ans . size ( ) != l ) { if ( cnt % 2 == 0 ) ans += s [ indr -- ] ; else ans += s [ indl ++ ] ; cnt ++ ; } } reverse ( ans . begin ( ) , ans . end ( ) ) ; return ans ; } int main ( ) { string s = " segosegekfrek " ; int l = s . length ( ) ; cout << decrypt ( s , l ) ; return 0 ; }