Write a program to determine if an input character string is of the form
xCy
where x is a string consisting of the letters ’A’ and ’B’, and where y is the reverse of x (that is, if x="ABABBA", y must equal "ABBABA"). At each point, you may read only the next character of the string.
Difficulty level
This exercise is mostly suitable for students
void main()
{
element c, e;
stack s;
int valid;
valid=1;
s=CreateStack();
do{
c=getchar();
if(c!='C')
Push(&s,c);
}while(c!='C');
while(Top(s,&e)==1 )
{
Pop(&s);
c=getchar();
if(c!=e)
valid = 0;
}
if(!isEmptyStack(s))
valid=0;
if(valid==1)
printf("Valid Expression");
else
printf("Invalid Expression");
getch();
return 0;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Checks if a substring is present in a string