Skip to content

Instantly share code, notes, and snippets.

@devpruthvi
Created November 16, 2015 06:24
Show Gist options
  • Save devpruthvi/8bf133406d20982bd0ab to your computer and use it in GitHub Desktop.
Save devpruthvi/8bf133406d20982bd0ab to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<string.h>
int checkState(char ele[20][20],char array[50][3],int arrlength){
int i;
for(i=0;i<arrlength;i++)
if(strcmp(ele[0],array[i]) ==0)
return i;
return -1;
}
void main(){
int symbolsNo,statesNo,i,j,start=0,end=0,test=0,count=0,finalNo;
char states[50][3],symbols[50][3],str[50][50][10],id[50],temp[20][20],final[50][3],temp1[20][20];
printf("Enter no of tests u want to perform:\n");
scanf("%d",&test);
printf("Enter number of states:\n");
scanf("%d",&statesNo);
printf("Enter number of symbols\n");
scanf("%d",&symbolsNo);
printf("Enter names of the states [start state - final state]\n");
for(i=0;i<statesNo;i++)
scanf("%s",states[i]);
printf("Enter names of the symbols\n");
for(i=0;i<symbolsNo;i++)
scanf("%s",symbols[i]);
printf("Enter transitions below:\n");
for(i=0;i<statesNo;i++){
for(j=0;j<symbolsNo;j++){
printf("( %s , %s ) = " ,states[i],symbols[j]);
scanf("%s",str[i][j]);
}
}
printf("Enter number of final states:\n");
scanf("%d",&finalNo);
printf("Enter final states:\n");
for(i=0;i<finalNo;i++)
scanf("%s",final[i]);
while(test != 0){
printf("\n\nEnter instantaneous discription to test the validity\n");
scanf("%s",id);
count = strlen(id);
start =0;
for(i=0;i < count;i++){
temp1[0][0] = id[i];
end =checkState(temp1[0],symbols,symbolsNo);
if(end == -1)
break;
strcpy(temp[0],str[start][end]);
start = checkState(temp,states,statesNo);
if(start == -1)
break;
}
if(i != count || checkState(temp,final,finalNo) == -1)
printf("Entered an invalid id");
else
printf("Entered a valid id");
test--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment