Advertisement
SalmaYasser

decode a string

Dec 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. string fun (int &idx, string &str)
  2. {
  3. string res = "";
  4. string decoded_string = "";
  5.  
  6. while (idx < str.size())
  7. {
  8.  
  9. if (isdigit(str[idx]))
  10. {
  11.  
  12. string num ="";
  13. while ( idx < str.size() && isdigit(str[idx] ) )
  14. num += str[idx++];
  15. int t = stoi (num);
  16.  
  17. idx++;
  18. decoded_string = fun (idx,str);
  19.  
  20. for (int ttimes = 1 ; ttimes <= t ; ttimes++)
  21. res += decoded_string;
  22.  
  23. }
  24. else if (isalpha(str[idx]) )
  25. res+= str[idx];
  26.  
  27. else if (str[idx] == ']')
  28. return res;
  29.  
  30. idx++;
  31.  
  32. }
  33. return res;
  34. }
  35. string decodeString(string s) {
  36. int idx = 0 ;
  37. return fun (idx ,s);
  38.  
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement