Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define N 100
- const char* strstr (const char* src, const char* target);
- int main(void)
- {
- char src[N];
- char target[N];
- std::cin >> src;
- std::cin >> target;
- const char* x = strstr (src, target);
- if (x == NULL)
- std::cout << -1;
- else
- std::cout << (int)(x-src);
- return 0;
- }
- const char* strstr (const char* src, const char* target)
- {
- int target_len = 0;
- for ( int i = 0; target[i] != 0; i++ )
- target_len += 1;
- int k = 0;
- for (int i = 0; src[i] != '\0'; i++) {
- if (src[i] == target[0]) {
- for (int j = 0; target[j] != '\0'; j++) {
- if (src[i+j] == target[j])
- k++;
- if (k == target_len)
- return &src[i];
- }
- }
- k = 0;
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement