Friday, April 12, 2019

Given NFA for (11+110)* 0. Write a program to find δ* (q0, 110) and δ* (q0, 11)

#include<stdio.h>
#include<string.h>                                                                                                        

int get_index(char states[],char a)
{
        int i;
        //check if state is present in set or not... if present then return its index
        for(i=0;states[i]!='\0';i++)
        {
                if(states[i]==a)
                {
                        return i;
                }
        }
        return -1;
}

void union_of_states(char src[],char des[])
{
        int i,len;

        for(i=0;src[i]!='\0';i++)
        {
                if(strchr(des,src[i])=='\0')
                {
                        len=strlen(des);
                        des[len]=src[i];
                        des[len+1]='\0';
                }
        }
}


int main()
{
        int no_of_states,pos_of_state;
        char states[5];
        char zero[5][5],one[5][5];
        char temp1[5],temp2[5];
        char str[10];
        char initial_state,choice;

        int i,j;

        //get total no. of states in NFA
        printf("Enter number of States: ");
        scanf("%d",&no_of_states);


        //get names of all states
        for(i=0;i<no_of_states;i++)
        {
                printf("\nEnter symbol for state %d: ",(i+1));
                scanf("\n%c",&states[i]);
        }
        states[i]='\0';


        //get 0 and 1 transition of every state
        for(i=0;i<no_of_states;i++)
        {
                        printf("\nEnter 0 transition of state %c: ",states[i]);
                        scanf("%s",&zero[i]);

                        printf("Enter 1 transition of state %c: ",states[i]);
                        scanf("%s",&one[i]);
        }

        //printing the table
        printf("\nState    0\t 1\n");
        printf("___________________\n");
        for(i=0;i<no_of_states;i++)
        {
                printf("%c \t %s \t %s\n",states[i],zero[i],one[i]);
        }

        while(1)
        {
                printf("Enter String to be checked.: ");
                scanf("%s",&str);

                temp1[0]=states[0];
                temp1[1]='\0';

                temp2[0]='\0';

                for(i=0;str[i]!='\0';i++)
                {
                        temp2[0]='\0';
                        for(j=0;temp1[j]!='\0';j++)
                        {
                                pos_of_state=get_index(states,temp1[j]);
                                if(str[i]=='0')
                                {
                                        union_of_states(zero[pos_of_state],temp2);
                                }
                                else
                                {
                                        union_of_states(one[pos_of_state],temp2);
                                }
                        }
                        strcpy(temp1,temp2);
                }
                printf("Final States are: %s\n",temp1);

                printf("\n\nCheck for new String?\nPress 'y' for yes and 'n' for no.\n");
                scanf("\n%c",&choice);
                if(choice=='n'||choice=='N')
                {
                        return;
                }
        }
        return 0;
}


ScreenShot:

No comments:

Post a Comment