Advertisement
Guest User

Regular Expression

a guest
Dec 11th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdio.h>
  3.  
  4. int main(){
  5.  
  6.     char text[256];
  7.     int i, length, error, no_rule;
  8.  
  9.     /*
  10.     Rule 1: a*b
  11.     Rule 2: ab+
  12.     Rule 3: abb
  13.     */
  14.  
  15. repeat:
  16.     i = 0;
  17.     length = 0;
  18.     error = 0;
  19.     no_rule = 1;
  20.  
  21.  
  22.     printf("Enter Text: ");
  23.     scanf("%[^\n]s", text);
  24.     length = strlen(text);
  25.  
  26.     if(strcmp(text, ";") == 0){
  27.         return 0;
  28.     }
  29.  
  30.     if(length > 0){
  31.         if((text[0] == 'a' || text[0] == 'b') && text[length-1] == 'b'){
  32.             error == 0;
  33.             for(i = 1; i < length - 1; i++){
  34.                 if(text[i] != 'a'){
  35.                     error = 1;
  36.                     break;
  37.                 }
  38.             }
  39.             if(error == 0){
  40.                 printf("\nUnder Rule 1");
  41.                 no_rule = 0;
  42.             }
  43.         }
  44.  
  45.         if(text[0] == 'a' && text[1] == 'b'){
  46.             error = 0;
  47.             for(i = 1; i < length; i++){
  48.                 if(text[i] != 'b'){
  49.                     error = 1;
  50.                     break;
  51.                 }
  52.             }
  53.             if(error == 0){
  54.                 printf("\nUnder Rule 2");
  55.                 no_rule = 0;
  56.             }
  57.         }
  58.  
  59.         if(strcmp(text, "abb") == 0){
  60.             printf("\nUnder Rule 3");
  61.             no_rule = 0;
  62.         }
  63.  
  64.         if(no_rule == 1){
  65.             printf("\nUnder no rule!");
  66.         }
  67.     }
  68.  
  69.     printf("\n\n");
  70.  
  71.     fflush(stdin);
  72.     goto repeat;
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement