Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (sanaelshazly):

Given a set of different types of paired symbols; determine whether it is balanced or not (the opening and closing version of each type are paired correctly). Any other type of characters may appear in the input will be neglected. For example: (**(*[*])****) is balanced, also [{}**[]**()]** is balanced. But ((*{*)*}) is unbalanced as well as [()***[]***{*(*)}. Write a C++ program that requires a string containing and expression as an input from the user and check for balanced bracket pairs using stack data structure.

OpenStudy (anonymous):

Is this a balanced or unbalanced? (*[**(*]**))

OpenStudy (sanaelshazly):

unbalanced !!

OpenStudy (anonymous):

right... so start off by keeping a track of the latest opening bracket and a counter of the total brackets opened. if a closing bracket matching the last opening bracket is the first occurence of a bracket, the get rid of the bracket and reduce total by one and keep going. if not, then unbalanced does it make sense?

OpenStudy (sanaelshazly):

:D actually I didn't understand a single word !! you mean I should write an if condition ?! or what ?!

OpenStudy (anonymous):

write a for loop to read each character of the string. for each character check if it is an opening bracket , a closing bracket or another character. if the bracket is opening increase counter if its closing bracket, check with the last opening bracket. if they match, great... decrease the counter and continue. if they don't match, then you have an unbalanced string and you exit the loop and the program if it is another character just continue to the next character is this better?

OpenStudy (sanaelshazly):

it's better yeah ! but i have a problem in writing the code !! i just can't get it.. how to implement it right ! =/

OpenStudy (anonymous):

it's straight forward... give it a try and let us know where you are stuck. I don't think anyone will write code for you without you trying first... else you can pay someone to do it... doesn't really make sense!

OpenStudy (sanaelshazly):

nooooooo i didn't mean that !! i tried it anywaysss it worked but not that good.. i'm going to fix it =D and show it to u =))

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!