0% found this document useful (0 votes)
9 views

Recognize Strings Cs

Uploaded by

gexeto3780
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Recognize Strings Cs

Uploaded by

gexeto3780
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment 2: Recognize Strings Under 'a', 'ab+', 'abb'**

Objective

To recognize whether the given input strings belong to the regular expressions a*, a*b+, or abb.

Algorithm

1. For a*: Ensure the string contains only 'a' or is empty.

2. For a*b+: Check if the string starts with zero or more 'a' followed by one or more 'b'.

3. For abb: Check if the string matches exactly "abb".

4. Use conditions to validate each string against the patterns.

Program (C)

Copy code

#include <stdio.h>

#include <string.h>

int match_a_star(const char *str) {

for (int i = 0; i < strlen(str); i++)

if (str[i] != 'a') return 0;

return 1;

int match_a_star_b_plus(const char *str) {

int i = 0;

while (str[i] == 'a') i++;

if (str[i] == '\0') return 0; // No 'b' found

while (str[i] == 'b') i++;


return str[i] == '\0';

int match_abb(const char *str) {

return strcmp(str, "abb") == 0;

int main() {

char input[100];

printf("Enter a string: ");

scanf("%s", input);

if (match_a_star(input))

printf("Matches a*\n");

else if (match_a_star_b_plus(input))

printf("Matches a*b+\n");

else if (match_abb(input))

printf("Matches abb\n");

else

printf("No match\n");

return 0;

Sample Output

plaintext
Copy code

Enter a string: abb

Matches abb

You might also like