Assume that a language uses /* and */ pair for comments.Write a C program to check for balanced /* and */ pair in a given text file.Use pointer implementation of the data structure.

Recommended Answers

All 5 Replies

Fine, I'll assume that you missed all the posts which tell you to post what you tried, and that we don't give you free homework answers just because you've mastered "ctrl-c" and "ctrl-v".

Member Avatar for iamthwee

Once you get round to the actual assignment you might want to look at using a stack or some other similiar datastructure thereof.

Why stack? Why "pointer implementation of the data structure"?
All you need: fgetc(f) to read a file char by char plus a level counter plus a simple logic:

search '/'...
if the next char is '*' then comment started, search '*' ...
and scan again...

... and so on.
Start from a simplest scanner (without inner comments counter)...

Member Avatar for iamthwee

>Why stack?

Why? Because it would probably be the simplest solution for finding matching pair comments, but then again there are many ways to skin a cat.

Since comments cannot be nested, a stack really is not necessary. A simple flag would suffice. Although a stack would be really helpful if you are looking for braces/parens/etc, things that can be nested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.