Regular Expression

Regular Expression: regular expressionregex or regexp (sometimes called a rational expression) is, in theoretical computer science and formal language theory, a sequence of characters that define a search pattern.*

*Reference: Wikipedia


Sample Code:

Q1: Program to implement Regular Expression For (a+b)aa*.

Solution:

#include<stdio.h>
#include<string.h>
void main()
{
    int i,j,k,s,c;
    char input[10];
    printf("\n\tProgram For RE (a+b)aa*::\n");
    printf("\nEnter String:");
    scanf("%s",input);
    s=strlen(input);
    j=0;    
    for(k=0;k<s;k++)
    {
        if(input[j]=='$')
        {
            j++;
        }
        else
            break;    
    }    
    if(input[j]=='a' || input[j]=='b')
    {
        j++;
        for(k=j;k<s;k++)
        {    
            if(input[j]=='$')
            {
                j++;
            }
            else
                break;
        }    
        if(input[j]=='a')
        {
            j++;    
            c=1;
            for(i=j;i<s;i++)
            {
                if(input[i]=='a' || input[i]=='$')
                {
                    c=1;
                }
                else
                {
                    printf("\nString Rejected\n");
                    c=0;
                    break;
                }
            }
        }
        else
            printf("\nString Rejected\n");
    }
    else
        printf("\nString Rejected\n");

    if(c==1)
        printf("\nString Accepted\n");
}


Output:

1:
Program For RE (a+b)aa*::

Enter String:aa

String Accepted

2:
Program For RE (a+b)aa*::

Enter String:aba

String Rejected

3:
Program For RE (a+b)aa*::

Enter String:ba

String Accepted

4:
Program For RE (a+b)aa*::

Enter String:a$a$a$

String Accepted

5:
Program For RE (a+b)aa*::

Enter String:$$$aa$$

String Accepted



Q2: Program to implement Regular Expression For a*+b*+c*.

Solution:

#include<stdio.h>
#include<string.h>
void main()
{
    int i,j,k,s,c;
    char input[10];
    printf("\n\tProgram For RE a*+b*+c*::\n");
    printf("\nEnter String::");
    scanf("\%s",input);
    s=strlen(input);
    j=0;
    for(i=0;i<s;i++)
    {
        if(input[i]=='$')
        {
            j++;
            c=1;
        }
        else
            break;
    }
    k=j;
    if(input[k]=='a')
    {
        for(i=j;i<s;i++)
        {
            if(input[i]=='a' || input[i]=='$')
            {
                j++;
                c=1;
            }
            else
            {
                c=0;
                printf("\nString Rejected\n");
                break;
            }
        }
    }
    else if(input[k]=='b')
    {
        for(i=j;i<s;i++)
        {
            if(input[i]=='b' || input[i]=='$')
            {
                j++;
                c=1;
            }
            else
            {
                c=0;
                printf("\nString Rejected\n");
                break;
            }
        }
    }
    else if(input[k]=='c')
    {
        for(i=j;i<s;i++)
        {
            if(input[i]=='c' || input[i]=='$')
            {
                j++;
                c=1;
            }
            else
            {
                c=0;
                printf("\nString Rejected\n");
                break;
            }
        }
    }
    else if(c==1)
    {
        c=0;
        printf("\nString Accepted\n");
    }
    else
    {
        c=0;
        printf("\nString Rejected\n");
    }
    if(c==1)
    {
        printf("\nString Accepted\n");
    }
}



Previous Post Next Post