Advertisement
Nahid8195

Prefix Suffix Substring in C

Dec 9th, 2022
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. int main()
  4. {
  5.     // Variable declaration and initialization
  6.     int i = 0;
  7.     bool flag = false;
  8.     int length = 0;
  9.     char str[100];
  10.  
  11.     // Take input from user
  12.     fgets(str, 100, stdin);
  13.  
  14.     // Prefix matching
  15.     if (str[0] == 'a' && str[1] == 'b')
  16.     {
  17.         printf("Prefix matched");
  18.     }
  19.     else
  20.     {
  21.         printf("Prefix not matched");
  22.     }
  23.  
  24.     printf("\n\n");
  25.  
  26.     // Suffix matching
  27.     length = strlen(str)-1;
  28.  
  29.     if (str[length - 2] == 'a' && str[length - 1] == 'b')
  30.     {
  31.         printf("Suffix matched");
  32.     }
  33.     else
  34.     {
  35.         printf("Suffix not matched");
  36.     }
  37.  
  38.     printf("\n\n");
  39.  
  40.     // Substring matching
  41.     for (i = 0; i < length; i++)
  42.     {
  43.         if (str[i] == 'a' && str[i + 1] == 'b')
  44.         {
  45.             flag = true;
  46.             break;
  47.         }
  48.     }
  49.  
  50.     if (flag)
  51.     {
  52.         printf("Substring matched");
  53.     }
  54.     else
  55.     {
  56.         printf("Substring not matched");
  57.     }
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement