Click here to Skip to main content
15,794,629 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Please guide me about this program, that will read a C source file and verify that the number of right and left braces in the file are equal. Use getc() function to read the file.

What I have tried:

studying about file handling.
Posted
Updated 16-Oct-17 1:57am

This looks like homework. Therefore, I (and other's here) will not write the code for you.

But this task is not difficult. Just try it yourself:
 
Share this answer
 
  1. Set a open (left) brace counter, say cnt=0.
  2. read a int, say c, with fgetc(). If c == -1 'end of file' has been reached, hence goto step 6.
  3. if c ==' {' then increment cnt.
  4. if c == '}' then decrement cnt. Check if (cnt < 0) and report failure (unbalanced brace) in such a case.
  5. goto step 1.
  6. if (cnt == 0) report success, otherwise report failure.
     
    Share this answer
     

    This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



    CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900