Advertisement
epic_caterpillar

findString.c

Feb 24th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. int findString(char source1[], char source2[]);
  4.  
  5. int main()
  6. {
  7.     char x[] = "a chatterbox";
  8.  
  9.     char y[] = "hat";
  10.  
  11.     int z = findString(x, y);
  12.  
  13.     printf("%d\n", z);
  14.  
  15.     return 0;
  16. }
  17.  
  18. int findString(char source1[], char source2[])
  19. {
  20.     int flag = 1;
  21.  
  22.     int i = 0;
  23.     int j = 0;
  24.     int counter = 0;
  25.  
  26.     while(source1[i] != '\0' && j < strlen(source2))
  27.     {
  28.         if(flag == 1)
  29.         {
  30.             if(source1[i] == source2[j])
  31.             {
  32.                 flag = 1;
  33.                 i++;
  34.                 j++;
  35.             }
  36.             else
  37.             {
  38.                 flag = 0;
  39.                 j = 0;
  40.             }
  41.         }
  42.         else
  43.         {
  44.             if(source1[i] == source2[j])
  45.             {
  46.                 flag = 1;
  47.                 counter = i;
  48.             }
  49.             else
  50.             {
  51.                 i++;
  52.             }
  53.         }
  54.     }
  55.     if(flag == 1)
  56.     {
  57.         return counter;
  58.     }
  59.     else
  60.         return -1;
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement