How to write a c program to read a parenthesised infix expression from the user and check whether it is well parenthesised or not in C Programming Language ?
Solution:
/*write a c program to read a parenthesised infix expression from the user and check whether it is well parenthesised or not */
#include<stdio.h>
#include<conio.h>
void main()
{
char infix[20];
int stack[20],i,cnt=0,n;
int c=0,d=0,top=0,j=0,k=0;
clrscr();
printf("\nenter infix expression:-");
gets(infix);
n=strlen(infix);
for(i=0;i<n;i++)
{
if(infix[i]=='(')
c++;
if(infix[i]==')')
d++;
if(infix[i]=='*'||infix[i]=='/'||infix[i]=='+'||infix[i]=='-')
{
if(infix[i+1]=='('||infix[i+2]==')')
stack[top++]=1;
else
stack[top++]=0;
}
if(infix[i]=='*'||infix[i]=='/'||infix[i]=='+'||infix[i]=='-')
{
if(infix[i-1]==')'||infix[i-2]=='(')
stack[top++]=1;
else
stack[top++]=0;
}
if(infix[i]=='(')
{
if(infix[i+1]==')'||infix[i+2]==')'||infix[i+3]==')')
stack[top++]=0;
}
if(infix[i]==')')
{
if(infix[i-1]=='('||infix[i-2]=='('||infix[i-3]=='(')
stack[top++]=0;
}
}
for(i=0;i<n;i++)
{
if(infix[i]=='*'||infix[i]=='/'||infix[i]=='+'||infix[i]=='-')
j++;
if(infix[i]==')'||infix[i]=='(')
k++;
}
cnt=0;
for(i=top-1;i>=0;i--)
{
if(stack[i]==1)
cnt++;
}
if(c==d&&cnt==top&&j>0&&k>0)
printf("\nthe given expression is well parenthesised");
else
printf("\nit is not well parenthesised please try again");
getch();
}
/*
enter infix expression((A+B)*(C-D))
the given expression is well parenthesised
*/
Learn More :
Not
Read
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C or C++ program to read marks of 4 subjects and find how many students are pass and fail with division
- C Program readers/writers
- C Program to Demonstrates use of reader/writer lock
- C Program to Write/Read Dynamic Data Example
- C Function to read instructions from the file and obey them
- A small I/O bound program to copy N bytes from an input file to an output file.
- Input reads in the array positions and range for random generation.
- C Program to Read and Write FIFO
- pthreads Readers/Writers Lock C Program
- C Program to opens and reads dictionary file specified in spellrc
- C Program reads in and spews out the attributes of a given .wav file.
- C Program Number of Judge and Score From Each Judge
- Read 10 real numbers using a vector and show the least of them C Program
- C Program To Read The Adjecancy Matrix of Directed Graph And Convert It Into Adjecancy List
- C Program Read a char & print next char ( file to file )
- Menu Driven Program to Read Two Integers Find Sum, Difference and Product C Program
Data Structure
- C program to remove last of singly linked list and insert it at beginning of list
- C Program To Remove Last Of Singly Linked List And Insert It At Beginning of List
- C Program To Remove First Node Of Singly Linked List And Insert It At End Of The List
- C Program To Read The Adjecancy Matrix of Directed Graph And Convert It Into Adjecancy List
- C Program To Create Two Singly Linked List and Perform Following Operation
- C Program - Hash Table to store information about a student
- Create Two Singly Linked List Perform Differences Display It C Program
- GJK C Program Example-1
- Adding two polynomial functions C Program Using Structure
- Program to Add Two Polynomials Using Linked List C Program
- Adding Two Polynomial Functions in C Program
Check
- CHECKING LEAP YEAR USING C PROGRAM.
- Check given number is prime number or not using c program.
- Write a c program to check given string is palindrome number or not.
- C program to Check While packet(s) hasn't been acked
- C Program to Check Prime Use Loop And Recursive
- Given a numerical value, check if at least one of the elements of the vector is equal to the numerical value if so, say where in the negative case, say that there is no.
- C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not
- C Program To Check The Given Value Is Vowel
- C Function to Check Vowel
- C Program To Check Leap Year
- C Program To Check If It Is A Palindrome Numbers Or Not
- C Program to Check if a number is in an array[1000]
- Armstrong Number Checker in C
- C Program to Check Given String is Palindrome or Not
- C Program to Check Whether Number Is Perfect Or Not
- C Program To Alarm Check
- Input a Number and Check if it's Palindrome or not C Program
- Input Number and Check if it's Armstrong C Program
- Input Number and Check if it's Even or Odd C Program
Enter by User
- C Program to Enter an ODD number between 1 and 49.
- C Program to Calculation of One Number Raised to Another
- C Program to Count Vowels, Consonants and Spaces in a string
- C Program to calculate Area, Perimeter of Rectangle; Area, Circumference of Circle.
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to find result where value and power are user given
- C Program That in a Given Natural Number x Insert Figure c at Position p
- C Program To Find Prime Number And If Number Is Not Prime Then Find Factors
- C program that lets the user choose if he wants to add a item to the shopping list or print it out
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to Input Number by the user between 1-100
- How to Gets User Input For Height in C Programming
- Loudness Program: Gets loudness level from user; chooses a response And prints it using logical and operative statements
- C Program Exchange User Entered Text Between 2 Modules
- C Program to accept n numbers from user & find out the maximum element out of them by using dynamic memory allocation
- C Program to accept string from user & convert all lower case letters into upper case letters & vice versa
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers
- C program to accept n number from user,store these numbers into an array & count the no of occurrences of each number
- C Program to accept n numbers from user store these numbers into an array & reverse an array elements using function
- C Program to accept a string from user, delete all vowels from that string & display the result
- Write a c program to accept a string from user & generate following pattern (e.g, input "abcd")
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
- C Program to accept n numbers from user, store these numbers into an array. Find out Maximum & Minimum number from an Array
Parenthesised