0% found this document useful (0 votes)
27 views12 pages

Untitled 1

The document contains C programs to validate identifiers using both regular expressions and basic character checks. It also includes programs to check if a given string matches specific regular expressions such as 'ab', '(a)*abb', '(ab)*a', and 'b(a)+b'. Each program prompts for user input and outputs whether the input matches the specified criteria.

Uploaded by

srinidhi.k2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

Untitled 1

The document contains C programs to validate identifiers using both regular expressions and basic character checks. It also includes programs to check if a given string matches specific regular expressions such as 'ab', '(a)*abb', '(ab)*a', and 'b(a)+b'. Each program prompts for user input and outputs whether the input matches the specified criteria.

Uploaded by

srinidhi.k2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Write a program to determine wether the given input is a valid identifier or not(with and
without regular expression)
i)input : sam , output: Valid identifier
ii)input: 3_ab output: Invalid identifier

C PROGRAM:
#include <stdio.h>
#include <regex.h>

int isValidIdentifier(const char* input) {


regex_t regex;
int result;

result = regcomp(&regex, "^[a-zA-Z_][a-zA-Z0-9_]*$", REG_EXTENDED);


if (result) {
printf("Could not compile regex\n");
return 0;
}

result = regexec(&regex, input, 0, NULL, 0);


regfree(&regex);

return !result;
}

int main() {
char input[100];

printf("Enter an identifier: ");


scanf("%s", input);

if (isValidIdentifier(input)) {
printf("Input: %s, Output: Valid identifier\n", input);
} else {
printf("Input: %s, Output: Invalid identifier\n", input);
}

return 0;
}

OUTPUT:

with regular expression


without regular expression:
#include <stdio.h>
#include <ctype.h>

int isValidIdentifier(const char* input) {


if (!isalpha(input[0]) && input[0] != '_') {
return 0;
}

for (int i = 1; input[i] != '\0'; i++) {


if (!isalnum(input[i]) && input[i] != '_') {
return 0;
}
}

return 1;
}

int main() {
char input[100];

printf("Enter an identifier: ");


scanf("%s", input);

if (isValidIdentifier(input)) {
printf("Input: %s, Output: Valid identifier\n", input);
} else {
printf("Input: %s, Output: Invalid identifier\n", input);
}

printf("Submitted by Rupesh_22BCE1909");

return 0;
}
2.Write a program to check wether a given string will get occupied for the below regular
expression
i)ab

CODE:
#include <stdio.h>
#include <string.h>

int matchesAB(const char* input) {


if (strlen(input) != 2) {
return 0;
}
if (input[0] == 'a' && input[1] == 'b') {
return 1;
}
return 0;
}

int main() {
char input[100];
printf("Enter a string: ");
scanf("%s", input);

if (matchesAB(input)) {
printf("Input: %s, Output: String is matching with the regular expression 'ab'\n", input);
} else {
printf("Input: %s, Output: String is not matching with the regular expression 'ab'\n", input);
}
return 0;
}

OUTPUT:

ii)(a)*abb

CODE:
#include <stdio.h>
#include <string.h>

int matchesAStarABB(const char* input) {


int length = strlen(input);
if (length < 3) return 0;
for (int i = 0; i < length - 3 + 1; i++) {
if (input[i] != 'a') return 0;
}
if (input[length - 3] == 'a' && input[length - 2] == 'b' && input[length - 1] == 'b') {
return 1;
}
return 0;
}

int main() {
char input[100];
printf("Enter a string: ");
scanf("%s", input);

if (matchesAStarABB(input)) {
printf("Input: %s, Output: String is matching with the regular expression '(a)*abb'\n", input);
} else {
printf("Input: %s, Output: String is not matching with the regular expression '(a)*abb'\n",
input);
}

printf("Submitted by RUPESH_22BCE1909");

return 0;
}

OUTPUT:
For valid inputs:
i)abb

ii)aabb
Faor invalid inputs:
i)aaaaabbb

ii)Rupesh
iii)(ab)*a

CODE:
#include <stdio.h>
#include <string.h>

int matchesABStarA(const char* input) {


int length = strlen(input);
if (length == 0 || input[length - 1] != 'a') return 0;
for (int i = 0; i < length - 1; i += 2) {
if (input[i] != 'a' || (i + 1 < length - 1 && input[i + 1] != 'b')) return 0;
}
return 1;
}

int main() {
char input[100];
printf("Enter a string: ");
scanf("%s", input);

if (matchesABStarA(input)) {
printf("Input: %s, Output: String matches the regular expression '(ab)*a'\n", input);
} else {
printf("Input: %s, Output: String does not match the regular expression '(ab)*a'\n", input);
}

printf("submitted by Rupesh(22bce1909)");

return 0;
}

OUTPUT:
VALID:

INVALID:
iv)b(a)+b

CODE:

#include <stdio.h>
#include <string.h>

int matchesBAplusB(const char* input) {


int length = strlen(input);
if (length < 3) return 0;
if (input[0] != 'b' || input[length - 1] != 'b') return 0;
for (int i = 1; i < length - 1; i++) {
if (input[i] != 'a') return 0;
}
return 1;
}

int main() {
char input[100];
printf("Enter a string: ");
scanf("%s", input);

if (matchesBAplusB(input)) {
printf("Input: %s, Output: String is matching with the regular expression 'b(a)+b'\n", input);
} else {
printf("Input: %s, Output: String is not matching with the regular expression 'b(a)+b'\n",
input);
}

printf("Submited by RUPES(22BCE1909)");
return 0;
}

OUTPUT:

VALID:

INVALID:

You might also like