You are on page 1of 2

Write a program for counting number of terminals and non-terminals in a given production.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char prod[30],cterm[10],cnterm[10];
int term=0,k=0,j=0;
int nterm=0;
cout<<"\n enter any production(e.g.:S->aDd)\n ";
gets(prod);
int length=strlen(prod);
for(int i=0;i<length;i++)
{
if(prod[i]>=65 && prod[i]<=90)
{
nterm++;
cnterm[j]=prod[i];
j++;
}
else if(prod[i]>=97 && prod[i]<=122)
{
term++;
cterm[k]=prod[i];
k++;
}
}
cout<<"\n terminals:";
puts(cterm);
cout<<"\n non-terminals:";
puts(cnterm);
cout<<"\n total number of terminals="<<term;
cout<<"\n total number of non-terminals="<<nterm;
getch();
}
OUTPUT
enter any production(e.g.:S->aDd)
S->abCDcE

terminals:abc

You might also like