/* String Recognition */
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#define SIZE_OF_STACK 30 //Number of slots in Stack
char alphabet[50];
int count=0;
class stack
{
private:
char slot[SIZE_OF_STACK];
int TOP;
public:
stack()
{
TOP=-1;
}
int PUSH(char item)
{
if(TOP==SIZE_OF_STACK-1)
{
return 0; // Push Operation Unsuccessful
}
else
{
TOP++;
slot[TOP]=item;
return 1; // Push Operation Successful
}
}
char POP(void)
{
if(TOP==-1)
{
return -1;
}
else
{
TOP--;
return slot[TOP+1];
}
}
void PEEP(int slot_number)
{
slot_number=slot_number-1;
if(slot_number>TOP&&slot_number<=SIZE_OF_STACK-1)
{
cout<<endl<<"ERROR : Item does not exist ! Last item is at Slot "<<TOP+1;
}
else if(slot_number<0)
{
cout<<endl<<"ERROR : Enter a valid Slot Number !";
}
else if(slot_number>SIZE_OF_STACK-1)
{
cout<<endl<<"ERROR : Slot number beyond bounds of stack ! Size of Stack is "<<SIZE_OF_STACK;
}
else
{
cout<<slot[slot_number];
}
}
int get_top()
{
return TOP;
}
};
void main()
{
clrscr();
stack s;
int no_of_alphabets=0;
int check_alphabet(char);
char next_alphabet(void);
char temp;
do
{
cout<<"How many alphabets does the language contain? : ";
cin>>no_of_alphabets;
if(no_of_alphabets<=0)
{
cout<<endl<<"ERROR : Enter a valid number (>0) !"<<endl<<endl;
}
}while(no_of_alphabets<=0);
for(int i=0;i<no_of_alphabets;i++)
{
cout<<endl<<"Alphabet "<<i+1<<" : ";
temp=getch();
putchar(temp);
if(check_alphabet(temp)==0)
{
cout<<" ERROR : Character already exists in the language !";
i--;
}
else
{
alphabet[i]=temp;
count++;
}
}
alphabet[i]='\0';
getch();
}
int check_alphabet(char x)
{
for(int i=0;i<count;i++)
{
if(x==alphabet[i])
{
return 0; // Character Repeated
}
}
return 1;
}
char next_alphabet(void)
{
static int number=0;
number++;
return alphabet[number];
}
Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.
