You are on page 1of 6

EXPERIMENT 1:

AIM: WAP in C++ for the regular expression


Objective:

Theory:
The three main components of a regular expression are anchors that are used to
specify the position of a pattern in relation to a line of text, character sets that
match one or more characters in a single position, and modifiers that specify the
number of times the previous character set is repeated.

The operations that help in building regular expressions are:

 Quantification: Quantifiers dictate how often the preceding element is


allowed to occur.
 Grouping: Operators can have their scope and precedence specified using
parentheses.
 Boolean Conditions: An OR or AND condition can be stated for
operators and groups.

Regular expressions use algorithms such as Deterministic Finite Automation


(DFA) and Non-deterministic Finite Automation (NFA) to match a string. In an
NFA, for each pair of state and input symbol there are several possible next
states, while a DFA accepts a finite string of symbols.
CODE:
1st program:
#include <iostream>
using namespace std;
void q0(string s, int i);
void q1(string s, int i);
void q2(string s, int i);
void q0(string s, int i)
{
cout<<"q0-";
if(i==s.length())
{
cout<<"invalid string";
return;
}
if(s[i]=='0')
{
q0(s,i+1);
}
else
{
q1(s,i+1);
}
}
void q1(string s, int i)
{
cout<<"q1-";
if(i==s.length())
{
cout<<"valid string";
return;
}
if(s[i]=='0')
{
q2(s,i+1);
}
else
{
q1(s,i+1);
}
}
void q2(string s, int i)
{
cout<<"q1-";
if(i==s.length())
{
cout<<"valid string";
return;
}
if(s[i]=='0')
{
cout<<"invalid string";
return;
}
else
{
cout<<"invalid string";
return
}
}
int main()
{
string s;
cout<<"Enter string: ";
cin>>s;
q0(s,0);
return 0;
}
OUTPUT:

2nd program:
#include <bits/stdc++.h>
using namespace std;

void q2(string s, int i)


{
if(i==s.length()){
cout<<"Valid String";
return;
}

if(s[i]=='c')
q2(s,i+1);
else
cout<<"Invalid String";
return;
}

void q1(string s, int i)


{
if(i==s.length())
{
cout<<"Invalid String\n";
return;
}

if(s[i]=='a' || s[i]=='b')
{
q1(s,i+1);
}

if(s[i]=='c' || s[i]=='d')
{
q2(s,i+1);
}
}

void q0(string s, int i)


{
if(i==s.length() || s[i]=='b' || s[i]=='d')
{
cout<<"Invalid String";
return;
}
if(s[i]=='c')
{
q2(s,i+1);
}

if(s[i]=='a')
{
q1(s,i+1);
}
}

int main()
{
string s;
cout<<"Enter the string: ";
cin>>s;

q0(s,0);
}
OUTPUT:

Result: strings were verified.

You might also like