Advertisement
Cinestra

Find Substring

Jan 12th, 2023
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. int find(char first_string[], char second_string[])
  6. {
  7.     for (int i = 0; i < strlen(first_string); i++)
  8.     {
  9.         int j;
  10.  
  11.         for (j = 0; j < strlen(second_string); j++)
  12.         {
  13.             if (first_string[i + j] != second_string[j])
  14.             {
  15.                 break;
  16.             }
  17.         }
  18.  
  19.         if (j == strlen(second_string))
  20.         {
  21.             return i;
  22.         }
  23.     }
  24.  
  25.     return -1;
  26. }
  27.  
  28. int main()
  29. {
  30.     char key[20] = "YoonA";
  31.     char code[50] = "I love YoonA a lot";
  32.  
  33.     int where_is_it;
  34.     where_is_it = find(code, key);
  35.  
  36.     cout << where_is_it;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement