New Member FAQ | Forums | Earn Revenue


Resources Entrance Ask Experts Exam Papers Jobs English Projects Universities Colleges Courses Schools Training My India



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



Resources » Articles/Knowledge Sharing » Computer & Technology »

C Program to check nesting of parentheses using stack


Posted Date: 24 Mar 2008    Resource Type: Articles/Knowledge Sharing    Category: Computer & Technology
Author: ashish singhMember Level: Gold    
Rating: 3 out of 53 out of 53 out of 5Points: 3



/* Program to check nesting of parentheses using stack */
#include
#define MAX 20
#define true 1
#define false 0
int top = -1;
int stack[MAX];

push(char);
char pop();

main()
{
char exp[MAX],temp;
int i,valid=true;
printf("Enter an algebraic expression : ");
gets(exp);

for(i=0;i {
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push( exp[i] );
if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
if(top == -1) /* stack empty */
valid=false;
else
{
temp=pop();
if( exp[i]==')' && (temp=='{' || temp=='[') )
valid=false;
if( exp[i]=='}' && (temp=='(' || temp=='[') )
valid=false;
if( exp[i]==']' && (temp=='(' || temp=='{') )
valid=false;
}/*End of else */
}/*End of for*/
if(top>=0) /*stack not empty*/
valid=false;

if( valid==true )
printf("Valid expression\n");
else
printf("Invalid expression\n");
}/*End of main()*/

push(char item)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
top=top+1;
stack[top] = item;
}
}/*End of push()*/

char pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
return(stack[top--]);
}/*End of pop()*/



Responses

Author: Prasad    18 May 2008Member Level: Gold   Points : 2
Thanks for this useful program


Author: Shanthi M    24 May 2008Member Level: Diamond   Points : 2
It is useful for balancing parenthesis.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: C Program of input and output restricted dequeue using array
Previous Resource: C Program for conversion of infix to postfix and evaluation of postfix
Return to Discussion Resource Index
Post New Resource
Category: Computer & Technology


Post resources and earn money!
 
More Resources



Advertise Here


Watch TV Channels




Contact Us   Advertise   Editors    Privacy Policy    Terms Of Use   

ISC Technologies.
2006 - 2009 All Rights Reserved.