/**
PPP18 ex#2:  Write a function,
char* findx(const char* s, const char* x),
that finds the first occurrence
of the C-style string x in s. Do not use any standard
library functions. Do not use subscripting;
use the dereference operator * instead.
*/

#include <iostream>
#include <string.h>
using namespace std;

char* findx(const char* s, const char* x) {
    int ns{0}, nx{0};
    while(*s++) ++ns;
    s -= ns+1;
    while(*x++) ++nx;
    x -= nx+1;
    int i = 0, j = 0;
    while(*(s+i)) { /// while not at the end of s
        while(*(x+j)) {
            if(!*(s+i+j)) return 0; ///got to the end of s without finishing x
            if(*(s+i+j) != *(x+j)) break; /// no match yet
            if(!*(x+j+1)) return const_cast<char*>(x); /// found it!
            ++j; /// look at next characters
        }
        ++i; /// go to next character in s
    }
}

int main() {
    string s;
    cout << "\nEnter string for s: ";
    getline(cin, s);
    string x;
    cout << "\nEnter string for x: ";
    getline(cin, x);
    if(findx(s.c_str(),x.c_str())) cout << "\nFound " << x << " in " << s;
    else cout << "\nThe string " << x << " not found in " << s;
}

/**

Enter string for s: utter doggerel

Enter string for x: dog

Found dog in utter doggerel
Process returned 0 (0x0)   execution time : 17.165 s
*/
