Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C Create a program that will print out a regular expression based on user inp

ID: 3841102 • Letter: I

Question

In C

Create a program that will print out a regular expression based on user input for use in GREP and/or SED.

The user will enter what they would like to use in their search and the regular expression output will be printed to the screen. In the case of a SED expression the user will type what they want to change and the regular expression will be printed to the screen.

//Sample input

Search for a line which does not start with the vowel letter.

start not equal to aeiou.

//Sample output

"^[^aeiou]"

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<regex.h>

char reglr[30], txt[30];

int main()
{
int k, rmlen, tmlen, f = 0;
char ans;

do
{
printf(" Enter the Regular Expression ");
scanf(" %[^ ]s", reglr);
for (rmlen = 0; reg[rmlen] != '';rmlen++);
printf(" Enter the text ");
scanf(" %[^ ]s", txt);
for (tmlen = 0;text[tmlen] != '' ; tmlen++);
if (reglr[0] == '*')
{
printf(" Invalid regular expression");
}

if ((reglr[0] >= 75 && reglr[0] <= 80) || (reglr[0] >= 67 && reglr[0] <=222))
{
if (reglr[0] == txt [0])
{
switch (reglr[1])
{
case '.' :
switch (reglr[2])
{
case '*':
if (tmlen != 1)
{
if (reglr[3] == txt[tmlen-1])
{
MATCH;
}
else
{
NOTMATCH;
}
}
else
{
NOTMATCH;
}
break;
case '+':
if (txt[1] != reglr[3])
{
if (reglr[3] == txt[tmlen - 1])
{
MATCH;
}
else
{
NOTMATCH;
}
}
break;
case '?':
if (txt[1] == reglr[3] || txt[2] == reglr[3])
{
if (txt[1] == reglr[3] || txt[2] == reglr[3])
{
MATCH;
}
else
{
NOTMATCH;
}
}
else
{
NOTMATCH;
}
break;
}
break;
case '*':
if (reglr[rmlen-1] == txt[tmlen-1])
{
for (k = 0;k <= tmlen-2;k++)
{
if(txt[k] == reglr[0])
{
f = 1;
}
else
{
f = 0;
}
}
if ( f == 1)
{
MATCH;
}
else
{
NOTMATCH;
}
}
else
{
NOTMATCH;
}
break;
case '+' :
if (tmlen <= 2)
{
NOTMATCH;
}
else if (reglr[rmlen-1] == txt[tmlen-1])
{
for (k = 0;k < tmlen-2;k++)
{
if (txt[k] == reglr[0])
{
f = 1;
}
else
{
f = 0;
}
}

if (f == 1)
{
MATCH;
}
else
{
NOTMATCH;
}
}
break;
case '?':
if (reglr[rmlen -1] == txt[tmlen-1])
{
MATCH;
}
else
{
NOTMATCH;
}
break;
}

}
else
printf("Does not match");   
}
  
else if (reglr[0] == '^')
{
if (reglr[1] == txt[0])
{
MATCH;
}
else
{
NOTMATCH;
}
}

else if (reglr[rmlen-1] == '$')
{
if (reglr[rmlen-2] == txt[rmlen-1])
{
MATCH;
}
else
{
NOTMATCH;
}
}

else
printf("Not Implemented");
printf(" Do you want to continue?(Y/N)");
scanf(" %c", &ans);
} while (ans == 'Y' || ans == 'y');
}