fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. char * doit(char * s)
  6. {
  7. int levl = 0;
  8. for(char * c = s; *c; ++c)
  9. {
  10. if (*c == '(')
  11. {
  12. levl++;
  13. if (levl == 2) *c = '['; else if (levl > 2) *c = '{';
  14. }
  15. else if (*c == ')')
  16. {
  17. if (levl == 2) *c = ']'; else if (levl > 2) *c = '}';
  18. levl--;
  19. if (levl < 0) return NULL;
  20. }
  21. }
  22. return (levl == 0) ? s : NULL;
  23. }
  24.  
  25. int main()
  26. {
  27. char s[] = "hgacsh(jhjh jhbhj ( uhghgv (bnv) jhih (jhjkh( lkjhjh))jhgjh) hjhj)ghj";
  28. char q[] = "hgacsh(jhjh jhbhj ( uhghgv (bnv) jh ) ih (jhjkh( lkjhjh))jhgjh) hjhj)ghj";
  29. char r[] = "hgacsh(jhjh jhbhj ( uhghgv (bnv) jh ( ih (jhjkh( lkjhjh))jhgjh) hjhj)ghj";
  30.  
  31. char * res = doit(s);
  32. if (res) printf("%s\n",res); else puts("Wrong string!");
  33.  
  34. res = doit(q);
  35. if (res) printf("%s\n",res); else puts("Wrong string!");
  36.  
  37. res = doit(r);
  38. if (res) printf("%s\n",res); else puts("Wrong string!");
  39.  
  40.  
  41. }
  42.  
Success #stdin #stdout 0s 4880KB
stdin
Standard input is empty
stdout
hgacsh(jhjh jhbhj [ uhghgv {bnv} jhih {jhjkh{ lkjhjh}}jhgjh] hjhj)ghj
Wrong string!
Wrong string!