C++ Programming Blog

 
 
 
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{

    int i=0,f=0,s=0;
    char arr[80];

    printf(\"Enter any input string terminated by $:\");
    scanf(\"%s\",arr);

    while(arr[i]!=\'$\' && f==0)
    {
        switch(s)
        {
            case 0:
                if(arr[i] >=\'0\' && arr[i] <=\'9\')
                {
                    s=1;
                    i++;
                }
                else

                    f=1;
                    break;

            case 1:
                 if(arr[i] >=\'0\' && arr[i] <=\'9\')
                 i++;
                 else if(arr[i]==\'.\')
                 {
                 s=2;
                 i++;
                 }
                 else
                 f=1;
                 break;

            case 2:
                if(arr[i] >=\'0\' && arr[i] <=\'9\')
                {
                    s=3;
                    i++;
                }
                else
                    f=1;
                    break;
              case 3:
                 if(arr[i] >=\'0\' && arr[i] <=\'9\')
                 i++;
                 else if(arr[i]==\'E\' || \'e\')
                 {
                 s=4;
                 i++;
                 }
                 else
                 f=1;
                 break;
            case 4:
                if(arr[i] == \'+\' || arr[i] == \'-\')
                {
                s=5;
                i++;
                }
                else
                f=1;
                break;

            case 5: if(arr[i] >=\'0\' && arr[i] <=\'9\')
                i++;
                else
                f=1;
                break;

        }

    }
      if(f==1)
    printf(\"\\nInvalid String !\");
      else
    printf(\"String is Valid \");

getch();
}




OUTPUT
***********
Enter any input string terminated by $:12.12E+12$
String is Valid

Enter any input string terminated by $:10.12E15$
Invalid String !




[/Code]

 
 
Didn't find what you were looking for? Find more on Program to display DFA