Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- int check(const char *s) {
- const int n = strlen(s);
- int counter = 0;
- for (int i = 0; i < n; ++i) {
- if (s[i] == '(') {
- counter ++;
- }
- if (s[i] == ')') {
- counter --;
- }
- if (counter < 0)
- return 0;
- }
- if (counter != 0)
- return 0;
- return 1;
- }
- void print_str(char* s) {
- const int n = strlen(s);
- if (check(s) == 0) {
- printf("check failed\n");
- return;
- }
- int counter = 0;
- for (int i = 0; i < n; ++i) {
- if (s[i] == '(') {
- counter ++;
- printf("%c", s[i]);
- continue;
- }
- if (s[i] == ')') {
- counter --;
- printf("%c", s[i]);
- continue;
- }
- if (counter == 0)
- printf("%c", s[i]);
- }
- }
- int main() {
- char str[4001];
- printf("enter string: ");
- scanf("%4001[^\n]", str);
- print_str(str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement