Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int round = 0, curly = 0, square = 0, errorflag = 0;
- int i, j, z;
- char c, *str = "", *tmp = NULL;
- for (i=0; (c = getchar())!= '\n'; i++)
- {
- tmp = str;
- str = (char*)malloc(i+2);
- strcpy(str, tmp);
- free(tmp);
- str[i] = c;
- str[i+1]= '\0';
- }
- for (j = 0; j!= strlen(str); j++)
- {
- if (str[j] == '(')
- {
- round++;
- for (z = j;z !=strlen(str);z++ )
- {
- if (str[z+1] == '}' || ']')
- {
- errorflag = 1;
- break;
- }
- if (str[z+1] == ')')
- {
- round--;
- break;
- }
- }
- }
- if (str[j] == '{')
- {
- curly++;
- for (z = j;z !=strlen(str);z++ )
- {
- if (str[z+1] == ')' || ']')
- {
- errorflag = 1;
- break;
- }
- if (str[z+1] == '}')
- {
- curly--;
- break;
- }
- }
- }
- if (str[j] == '[')
- {
- square++;
- for (z = j;z !=strlen(str);z++ )
- {
- if (str[z+1] == '}' || ')')
- {
- errorflag = 1;
- break;
- }
- if (str[z+1] == ']')
- {
- square--;
- break;
- }
- }
- }
- }
- if (round && curly && square == 0)
- printf("Here are unclosed round = %d\n curly = %d\n square = %d\n", round, curly, square);
- else
- {
- if(errorflag == 1)
- printf("Wrong brackets\n");
- else
- printf("Brackets aren't closed!\n");
- }
- // printf("%s\n i=%d", str, i);
- free(str);
- return 0;
- }
Add Comment
Please, Sign In to add comment