MCPcopy Create free account
hub / github.com/SR-Sunny-Raj/Hacktoberfest2021-DSA / decodeString

Function decodeString

11. Stack/DecodeString.cpp:20–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18using namespace std;
19
20string decodeString(string s) {
21 stack<int> countStack;
22 stack<string> wordStack;
23
24 int number = 0;
25 string word = "";
26 for (int i = 0; i < s.length(); ++i) {
27 char ch = s[i];
28 if (isdigit(ch)) {
29 number = number * 10 + (ch - '0');
30 }
31 else if (isalpha(ch)) {
32 word += ch;
33 }
34 else if (s[i] == '[') {
35 wordStack.push(word);
36 countStack.push(number);
37 word = "";
38 number = 0;
39 }
40 else { //If s[i] is ']'
41 int topMult = countStack.top(); countStack.pop();
42 string topS = wordStack.top(); wordStack.pop();
43 for (int i = 0; i < topMult; i++) {
44 topS.append(word);
45 }
46 word = topS;
47 }
48 }
49 return word;
50}
51
52int main() {
53

Callers 1

mainFunction · 0.85

Calls 4

pushMethod · 0.45
topMethod · 0.45
popMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected