QUESTION-21 Implement Regular Expression Matching
Answer-
#define MATCH printf("\nThe Text Matches The Regular Expression"); #define NOTMATCH printf("\nThe Text Doesn't match the Regular Expression"); char reg[20], text[20]; int main() { int i, rlen, tlen, f = 0; char ans; do { printf("\nEnter the Regular Expression\n"); scanf(" %[^\n]s", reg); for (rlen = 0; reg[rlen] != '\0';rlen++); printf("\nEnter the text\n"); scanf(" %[^\n]s", text); for (tlen = 0;text[tlen] != '\0' ; tlen++); if (reg[0] == '*') { printf("\nInvalid regular expression"); } if ((reg[0] >= 65 && reg[0] <= 90) || (reg[0] >= 97 && reg[0] <=122)) { if (reg[0] == text [0]) { switch (reg[1]) { case '.' : switch (reg[2]) { case '*': if (tlen != 1) { if (reg[3] == text[tlen-1]) { MATCH; } else { NOTMATCH; } } else { NOTMATCH; } break; case '+': if (text[1] != reg[3]) { if (reg[3] == text[tlen - 1]) { MATCH; } else { NOTMATCH; } } break; case '?': if (text[1] == reg[3] || text[2] == reg[3]) { if (text[1] == reg[3] || text[2] == reg[3]) { MATCH; } else { NOTMATCH; } } else { NOTMATCH; } break; } break; case '*': if (reg[rlen-1] == text[tlen-1]) { for (i = 0;i <= tlen-2;i++) { if(text[i] == reg[0]) { f = 1; } else { f = 0; } } if ( f == 1) { MATCH; } else { NOTMATCH; } } else { NOTMATCH; } break; case '+' : if (tlen <= 2) { NOTMATCH; } else if (reg[rlen-1] == text[tlen-1]) { for (i = 0;i < tlen-2;i++) { if (text[i] == reg[0]) { f = 1; } else { f = 0; } } if (f == 1) { MATCH; } else { NOTMATCH; } } break; case '?': if (reg[rlen -1] == text[tlen-1]) { MATCH; } else { NOTMATCH; } break; } } else printf("Does not match"); } else if (reg[0] == '^') { if (reg[1] == text[0]) { MATCH; } else { NOTMATCH; } } else if (reg[rlen-1] == '$') { if (reg[rlen-2] == text[rlen-1]) { MATCH; } else { NOTMATCH; } } else printf("Not Implemented"); printf("\nDo you want to continue?(Y/N)"); scanf(" %c", &ans); } while (ans == 'Y' || ans == 'y'); }
QUESTION-22 Implement strpbrk() Function
Answer-
char* strpbrk(char *, char *); int main() { char string1[50], string2[50]; char *pos; printf("Enter the String:\n"); scanf(" %[^\n]s", string1); printf("\nEnter the Character Set:\n"); scanf(" %[^\n]s", string2); pos=strpbrk(string1, string2); printf("%s", pos); } char* strpbrk(char *string1, char *string2) { int i, j, pos, flag = 0; for (i = 0; string1[i] != '\0';i++); pos = i; for (i = 0;string2[i] != '\0';i++) { for (j = 0;string1[j] != '\0';j++) { if (string2[i] == string1[j]) { if ( j <= p1) { pos = j; flag = 1; } } } } if (flag == 1) { return &string1[pos]; } else { return NULL; } }
QUESTION-23 Sort String Ignoring Whitespaces and Repeating Characters Only Once
Answer-
#define SIZE 50 void main() { char string[SIZE], string1[SIZE], string2[SIZE]; int i, j = 0, a = 0, temp, len = 0, len1 = 0, k = 0; printf("\nEnter a string:"); scanf("%[^\n]s", string1); for (i = 0;string1[i] != '\0';i++) { if (string1[i] == ' ') { continue; } string[j++] = string1[i]; } for (i = 0;string[i] != '\0';i++) { for (j = i + 1;string[j] != '\0';j++) { if (string[i] > string[j]) { temp = string[i]; string[i] = string[j]; string[j] = temp; } } } string[i] = '\0'; len = strlen(string); for (i = 0;string[i] != '\0';i++) { if (string[i] == string[i + 1] && string[i + 1] != '\0') { k++; continue; } string2[a++] = string[i]; string[a] = '\0'; } len1 = len - k; printf("The sorted string is:"); for (temp = 0;temp < len1;temp++) { printf("%c", string2[temp]); } }