Find us on Google+ Kill the code: string validation
Showing posts with label string validation. Show all posts
Showing posts with label string validation. Show all posts

Tuesday, 13 March 2012

Dynamic Finite Automata for String Validation in C

#include<conio.h>

#include<stdio.h>

void main()

{

 int tab[10][10], i, j, k, t=65, b, r, c, l=0;

 char s[20], t1[50];

 clrscr();

 printf("enter rows and cols : ");

 scanf("%d",&r);

 scanf("%d",&c);

 printf("enter your state transition table : ");

 scanf("%s",t1);

 for(i=0; i<r; i++)

 {

  for(j=0; j<c; j++)

  {

   tab[i][j]=t1[l];

   l++;

  }

 }

 for(i=0; i<r; i++)

 {

  for(j=0; j<c; j++)

  {

   printf("%c", tab[i][j]);

  }

  printf("\n");

 }

 printf("enter a string : ");

 scanf("%s",s);

 for(i=0; i<strlen(s); i++)

 {

  for(k=0; k<r; k++)

  {

   if(t==tab[k][0]) b=k;



  }

  if(s[i]=='0')

  {

   t=tab[b][1];

  }

  if(s[i]=='1')

  {

   t=tab[b][2];

  }

 }

 if(t==tab[r-1][0]) printf("valid");

 else printf("invalid");

 getch();

}