hi all,
i am really stuck with c++ pointer issues.
what my program is supposed to do is to take input from user and recognize whether the entered string is a valid word from the grammer S::= a|(S)
hence grammer should accept words like...
a, (a), ((a)),(((a)))....
i have done a bit of work on it, but my code seems to just stuck in an infinite loop or something. i think the error might lie in the pointer increments, but just cant place it rite.
have a look at code(its half done)
thanks in advance
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
void recognizer();
void checkValidWord();
char word[]="\0";
char *wordptr;
int openBraceCount=0,closeBraceCount=0;
void main(){
clrscr();
cout<<" Enter word"<<"\n";
cin>>word;
wordptr=&word[0];
cout<<word;
checkValidWord();
getche();
}
void checkValidWord(){
int length;
length= strlen(word);
if(length == '2' && word[0] == 'a' && word[1] == '\0')
cout<<" \n Valid Word ";
if(length % 2==0)
cout<<" \n Invalid Word ";
recognizer();
if(openBraceCount == closeBraceCount)
cout<<" \n Valid Word ";
getche();
}
void recognizer(){
while( *wordptr != '\0')
{
if(strchr("a", *wordptr)){
wordptr=wordptr++;
recognizer();
}
else{
if(strchr("(", *wordptr)){
openBraceCount += 1;
wordptr=wordptr++;
recognizer();
}
}
if(strchr(")", *wordptr)){
closeBraceCount += 1;
wordptr=wordptr++;
recognizer();
}
}
}