Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define N 100
  4.  
  5. const char* strstr (const char* src, const char* target);
  6.  
  7. int main(void)
  8. {
  9.     char src[N];
  10.     char target[N];
  11.    
  12.     std::cin >> src;
  13.     std::cin >> target;
  14.    
  15.     const char* x = strstr (src, target);
  16.     if (x == NULL)
  17.         std::cout << -1;
  18.     else
  19.         std::cout << (int)(x-src);
  20.     return 0;
  21. }
  22.  
  23. const char* strstr (const char* src, const char* target)
  24. {
  25.     int target_len = 0;
  26.     for ( int i = 0; target[i] != 0; i++ )
  27.         target_len += 1;
  28.        
  29.     int k = 0;
  30.        
  31.     for (int i = 0; src[i] != '\0'; i++) {
  32.         if (src[i] == target[0]) {
  33.             for (int j = 0; target[j] != '\0'; j++) {
  34.                 if (src[i+j] == target[j])
  35.                     k++;
  36.                 if (k == target_len)
  37.                     return &src[i];
  38.             }
  39.         }  
  40.         k = 0;
  41.     }
  42.     return NULL;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement