Created
March 16, 2019 09:52
-
-
Save aswinmprabhu/7a9f19452d50907047e404d75f2cab32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
string nstr; | |
string decode(string str, int *i) | |
{ | |
string retval; | |
while (*i < str.length() && str[*i] != ']') | |
{ | |
if (!isdigit(str[*i])) | |
{ | |
if (str[*i] == '[') | |
{ | |
int n = stoi(nstr); | |
nstr = ""; | |
*i += 1; | |
string istr = decode(str, i); | |
*i += 1; | |
while (n-- > 0) | |
{ | |
retval += istr; | |
} | |
} | |
else | |
retval += str[(*i)++]; | |
} | |
else | |
{ | |
nstr += str[(*i)++]; | |
} | |
} | |
return retval; | |
} | |
int main() | |
{ | |
string str; | |
cout << "Enter the string to be decoded : "; | |
cin >> str; | |
int i = 0; | |
string ans = decode(str, &i); | |
cout << ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment