P438 Computer Networks

Programming Assignment 1

Assigned:  August 23, 2012

Due: August 31, 2012 at 11:59 pm

 

Instructions:

·         You may discuss with your classmate, but please submit your own individual assignment.

·         Please submit your code via oncourse

·         If you have debug statements, please remove them or comment them out before submitting.

·         In addition to C files, supply a makefile and address all compilation errors.

 

Balanced Parentheses:

A common problem for compilers and text editors is to determine if the parentheses (or other brackets) in a string are balanced and properly nested.  For example, the string “((())())()” contains properly nested pairs of parentheses, but the string “)()(“ does not.  The string “())” does not contain properly matching parentheses.

A.    Write a program that returns TRUE if a string contains properly nested and balanced parentheses, and FALSE if otherwise.  Hint: At no time while scanning a legal string from left to right will you have encountered more right parentheses than left parentheses.

B.     If the string is unbalanced, your program should also return the position in the string of the first offending parentheses. For example, if an excess right parenthesis is found, return its position.  If there are too many left parentheses, return the position of the first excess left parenthesis.

C.     Your program should be able to accept input from the command line and from a file.  Your submission should explain how to run the program and indicate the values used to denote command line and file.

D.    Sample Input and Output

a.       ((())())()        True

b.      )()(                False 1

c.       ()(                 False 3