/* String Recognition */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define SIZE_OF_STACK 50
char alphabet[50]="";
char input_string[50]="";
int no_of_alphabets=0,i;
struct stack
{
char slot[SIZE_OF_STACK];
int TOP;
};
int push(st,item)
struct stack *st;
char item;
{
if(st->TOP==SIZE_OF_STACK-1)
{
return 0; /* Push Operation Unsuccessful (Stack Overflow) */
}
else
{
st->TOP++;
st->slot[st->TOP]=item;
return 1; /* Push Operation Successful */
}
}
char pop(st)
struct stack *st;
{
if(st->TOP==-1)
{
return -1; /* Stack Underflow */
}
else
{
(st->TOP)--;
return st->slot[st->TOP+1];
}
}
int check_alphabet(c)
char c;
{
int temp;
for(temp=0;temp<=i;temp++)
{
if(c==alphabet[temp])
{
return 0; /* Character Repeated */
}
}
return 1; /* Character Okay */
}
char next_char(string)
char string[50];
{
static int count=0;
count++;
return string[count-1];
}
main()
{
char temp,next;
struct stack s;
int x;
s.TOP=-1;
clrscr();
printf("\nHow many alphabets does the language have ? : ");
scanf("%d",&no_of_alphabets);
for(i=0;i<no_of_alphabets;i++)
{
printf("\nAlphabet %d : ",i+1);
temp=getche();
if(check_alphabet(temp)==1)
{
alphabet[i]=temp;
}
else
{
printf(" ERROR : Character already exists !");
i--;
}
}
alphabet[i]='\0';
printf("\n\nEnter Input String : ");
scanf("%s",input_string);
push(&s,alphabet[i-1]);
next=next_char(input_string);
while(next!=alphabet[i-1])
{
if(next=='\0')
{
printf("\nInvalid String !");
goto exit;
}
else
{
push(&s,next);
}
next=next_char(input_string);
}
while(s.slot[s.TOP]!=alphabet[i-1])
{
next=next_char(input_string);
temp=pop(&s);
if(next!=temp)
{
printf("\nInvalid String !");
goto exit;
}
}
next=next_char(input_string);
if(next=='\0')
{
printf("\nValid String !");
}
else
{
printf("\nInvalid String !");
}
exit:
getch();
}
Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.
