Question: :C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C

:C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C Programming C You are going to write a program to check whether the parentheses are balanced or not. For example, "( 0) ()" is a balanced parentheses, while "( 0) (" and "( O))" 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 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. Sample 1 Input: (0) Sample 1 Output: There are 3 balanced parentheses. Sample 2 Input: (0) Sample 2 Output: Error is spot at position 5 at first. #include #include #include #include #include int main (int argc, char *argv[]){ FILE * fin, *fout; int i, len; char s[102]; int count = 0; int error = -1; fin = fopen(argv[1], "r"); fout = fopen(argv[2], "W"); /* initialization of variables */ memset(s,o,sizeof(s)); /* input */ fscanf(fin, "%s", s); len = strlen(s); /* Your code here */ /* output format, you may need to edit the variables */ if (error == -1) { fprintf(fout, "There are %d balanced parentheses.", count); } else { fprintf(fout, "Error is spot at position %d at first.", error); } fclose(fin); fclose(fout); return 0; ]

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!