☞ Read an expression and count number of valid parenthesis.(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]=='('){
count++;
j--;
}
}
printf("%d",count);
}
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]=='('){
count++;
j--;
}
}
printf("%d",count);
}
Input
((A*B)+(C/D)-E)
Output
3
((A*B)+(C/D)-E)
Output
3