Objective:: To Check A String Is Accepted or Rejected by Regular Expression

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Objective: To check a string is accepted or rejected by regular expression.

Objective: To check a string is accepted or rejected by regular expression.


Introduction: Regular expressions are a standardized way to express patterns to be matched against
sequences of characters. They are a powerful way to find and replace strings that take a defined format. For
example, regular expressions can be used to parse dates, urls and email addresses, log files, configuration
files, command line switches or programming scripts.

Requirements:
1. Codeblocks

Description: In this lab task we have to determine a string accepted or rejected by a pattern following (a|
b)*abb. By this we can understand that if a string contains abb following a or b before it. To solve this
problem first we take input and create a loop until the string length. In the loop we have to check whether a
string contains a or b. If the condition is true then check next character is a or not. If condition is true then
check next two character is b or not. If the condition is true print the string is accepted. If the condition is not
true then the string is not accepted and once this condition is true exit the program.

Methodology:
start Input a string Inside
Create a loop until the thelength
string loop check a character of that string contain

If condition is true then If the condition is true


If the condition is true print the string is accepted. check next two then check next character
character is b or not. is a or not

If the condition is not true then


the string is not accepted and once
this condition is true exit the End
program.

Code:
#include <iostream>
using namespace std;
int main()
{
string a;
bool b;
cout<<"Enter a string to check: ";
cin>>a;
for(int i=0; i<a.length(); i++)
{
if(a[i] == 'a')
{
if(a[i+1] == 'a' || a[i+1] == 'b')
{
if(a[i+2] == 'b')
{
b=true;
}
else if(a[i+2] == 'a')
{
if(a[i+2] == 'b')
{
b=true;
}
}
else
{
cout<<"String Not Accepted"<<endl;
exit(0);
}
}
else
{
cout<<"String Not Accepted"<<endl;
exit(0);
}
}
}
if(b)
{
cout<<"String Accepted"<<endl;
}
}

Output:

Discussion: In this lab, our task was to check a string is accepted or rejected following a
pattern. Our program works fine almost every input that we checked. Although we faced
some problems but we solve it by change our program logic.

You might also like