For Loop Counter Going to 0

Can anyone help me with a problem? My program is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <iostream>
#include <cstdlib>

char *strstr(char *s1, char *s2)
{
    while (*s1 != '\0') {
        for (int i = 0; *(s2+i) != '\0'; ++i) {
            std::cout << i << std::endl;
            if (*(s1+i) != *(s2+i)) {
                return 0;
            }
        }
        ++s1;
    }
    return s2;
}

int main(int argc, char *argv[])
{
    char *s1 = "hello, world";
    char *s2 = "hel";
    char *result = strstr(s1, s2);
    if (result)
        std::cout << "string found" << std::endl;
    else
        std::cout << "string not found" << std::endl;
    return EXIT_SUCCESS;
}

For some reason, the output of this program is:
1
2
3
4
0
1
2
0

Why is i going back to 0, and why is this executing 4 times, not 3? Thanks!
It goes back to 0 because you enter the while loop a second time after performing the for loop three times.

Second time into the while loop *(s1) is 'e' which does not equal *(s2) == 'h' so you hit the return in the for loop after printing i==0.



I'm not even sure what the hell is going on there. I looked at it and though, WTF is that! (no offense)

Maybe someone who is a pointer expert can fully explain what is going on there but to remedy that code I would try something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char *strstr(char *s1, char *s2)
{
    int i = 0;
    while (s1[i] != '\0') {
        for (; s2[i] != '\0'; ++i) {
            std::cout << i << std::endl;
            if (s1[i] != s2[i]) {
                return 0;
            }
        }
        ++s1;
    }
    return s2;
}



P.S - Just know that this is not what C++ should look like. This is what C look like.
Last edited on
I'm not even sure what the hell is going on there. I looked at it and though, WTF is that! (no offense)

Maybe someone who is a pointer expert can fully explain what is going on there but to remedy that code I would try something like this:


No idea what's going on there? It's literally the same thing as what you posted except it uses pointer arithmetic instead of the subscript operator.

And what you posted doesn't remedy it. Line 11 screws over everything, and the logic is flawed (as it is in my OP) because it should be a break statement on line 8, a return 0 on line 13 and check if s2[i+1] == '\0' somewhere in the for loop, returning s2 if that is true.

How is what either of us posted what C should look like, but not C++? Yes, pointers are more commonly used in C, but what does that have to do with it? This program is valid in either language.

Hopefully this clears up your "WTF IS THAT??!?!?!!!111one"

Thank you, vin, for pointing out the problem in my program, and I've gotten it to work since then.
Last edited on
Your logic was indeed flawed but my code fixed your immediate problem.
Your logic was indeed flawed but my code fixed your immediate problem.


Your logic was also flawed, and what you posted didn't fix my problem. The "solution" you posted was completely broken. All you've done in this post is admit that you are ignorant because the second you saw pointer arithmetic you shouted out "WTF IS THAT," thought it was using some obscure language feature, and insulted me.

So now that you've 1) provided broken solutions and claimed that they work, 2) proved your ignorance of C++ and 3) denied it when I pointed out obvious flaws in your program, I'm going to mark this thread as solved (thank you, vin), laugh at you and ignore all subsequent posts you make.
Last edited on
Topic archived. No new replies allowed.