How could I get this code working correctly. At the moment I am only checking the amount of braces but I want to check their orders as well. In this case they are matching but normally it should give me an error. What's is the correct way to fix this or can some1 show me another way to do it. Thank you.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
int square=0, curly=0, round=0;
int i;
char userstr[21] = "(asdf[sd)ds]nk";
for(i=0;i<strlen(userstr);i++)
{
if(userstr[i]=='(')
round++;
else if(userstr[i]==')')
round--;
else if(userstr[i]=='{')
curly++;
else if(userstr[i]=='}')
curly--;
else if(userstr[i]=='[')
square++;
else if(userstr[i]==']')
square--;
}
if(round!=0 ||square !=0 || curly!=0){
printf("They are not matching try again\n");
}
else
printf("They are matching\n");
return 0;
}