/* Parentheses You may have heard of the programming language Lisp, or its more modern descendant, Scheme. One inescapable fact of Lisp programming is that you must use a lot of parentheses. Suppose that you've just started learning Lisp. You keep writing expressions in which you forget to close some of the parenthesis. Write a program that validates that for every left-parenthesis character '(' input to your program, there is a corresponding right-parenthesis character ')'. Hint: All inputs will be a single line of characters terminated with the newline character. ============= START OF SAMPLE RUN ======================== ---------------- START OF INPUT -------------------------- (+ 7 (* 5 (+ 1 4))) ----------------- END OF INPUT --------------------------- ---------------- START OF OUTPUT ------------------------- Valid Input ----------------- END OF OUTPUT -------------------------- ============= END OF SAMPLE RUN ========================== ============= START OF SAMPLE RUN ======================== ---------------- START OF INPUT -------------------------- ((((()))) ----------------- END OF INPUT --------------------------- ---------------- START OF OUTPUT ------------------------- Invalid Input ----------------- END OF OUTPUT -------------------------- ============= END OF SAMPLE RUN ========================== ============= START OF SAMPLE RUN ======================== ---------------- START OF INPUT -------------------------- )( ----------------- END OF INPUT --------------------------- ---------------- START OF OUTPUT ------------------------- Invalid Input ----------------- END OF OUTPUT -------------------------- ============= END OF SAMPLE RUN ========================== ============= START OF SAMPLE RUN ======================== ---------------- START OF INPUT -------------------------- (cdr (car '((peas carrots tomatoes) (pork beef chicken)))) ----------------- END OF INPUT --------------------------- ---------------- START OF OUTPUT ------------------------- Valid Input ----------------- END OF OUTPUT -------------------------- ============= END OF SAMPLE RUN ========================== */ #include int main(const int argc, const char* argv[]) { int c; unsigned long left = 0; unsigned long right = 0; do { c = getchar(); if (c == '(') { left++; } else if (c == ')') { right++; } if(right>left) break; } while (c != '\n' && c != EOF); if (left == right) { printf("Valid Input\n"); } else { printf("Invalid Input\n"); } system("pause"); return 0; }