☞ Read an expression and check whether expression contains valid parenthesis or not.(Nov,2018)
Solution in C PYTHON
Solution in C PYTHON
#include<stdio.h>
int main(){
char str[50],top[50];
int i,j=0,count=0;
fgets(str,50,stdin);
for(i=0;str[i]!='\0';i++){
if(str[i]=='(')
top[j++]=str[i];
else if(str[i]==')' && top[j-1]=='(')
j--;
}
if(j==0)
printf("Valid");
else
printf("Invalid");
return 0;
}
int main(){
char str[50],top[50];
int i,j=0,count=0;
fgets(str,50,stdin);
for(i=0;str[i]!='\0';i++){
if(str[i]=='(')
top[j++]=str[i];
else if(str[i]==')' && top[j-1]=='(')
j--;
}
if(j==0)
printf("Valid");
else
printf("Invalid");
return 0;
}
Input
((A*B)+(C/D)-E)
Output
Valid
((A*B)+(C/D)-E)
Output
Valid