Question: Question 3 : Matching Parentheses ( 3 0 % ) You are going to write a program to check whether the parentheses are balanced or

Question 3: Matching Parentheses (30%)
You are going to write a program to check whether the parentheses are balanced or not. For
example, (())() is a balanced parenthese, while (())( and (())) are
unbalanced. There are three types of parentheses, which are (),[] and {}. If the
parentheses are balanced, you need to output the number of matching parentheses. If the
parentheses are unbalanced, you need to output the first position (zero-based) that you spot
an error.
Your program should read the input from the file, and output the answer to another file. The
first argument is the input file name, while the second argument is the output file name.
Name your program as lab3-q3.c.
Input file: A string contains (),[] and {} only. The length of input will not exceed 100
characters.
Output file: If the parentheses are balanced, output There are X balanced
parentheses. If the parentheses are unbalanced, output Error is spot at position
Y at first. Y is a zero-based number. For the Sample 2 below, the error occurs at the
position of \0.
Sample 1 Input:
(())()
Sample 1 Output:
There are 3 balanced parentheses.
Sample 2 Input:
(())(
Sample 2 Output:
Error is spot at position 5 at first.
Sample 3 Input:
([)]
Sample 3 Output:
Error is spot at position 2 at first.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!