0

I am trying to search for a string in a string pointer. I am not sure how to explain exactly what I am trying to achieve, but I will try my best.

Let's say I have two string pointers char *p = "hello world" and char *q = "world", as world is in hello world, so I want to return a pointer *start that points to the w of *p. I hope that makes sense, if not then feel free to comment below.

Note: Unfortunately, I am not allowed to use any string.h library functions except strlen(), and also I am not allowed to use indexing in fact I am not allowed to use any square brackets [].

P.S. Ignore all the printf as they are there to check where the program is going wrong.

#include <stdio.h>
#include <string.h>

int main(){
    char *p = "hello world", *q = "world", *start;
    int i, count = 0;
    printf("%c\n", *p);
    for (i = 0; i < strlen(p); i++){
        printf("p: %c compares with q: %c\n", *p, *q);
        if ( *p == *q){ // tried: if ( p == q)
            start = p;
            printf("start: %s\n", start);
            while (*p == *q){ // tried: while ( p == q)
                printf("p: %c compares with q: %c\n", *p, *q);
                count ++;
                p ++;
                q ++;
                if ( count == strlen(q) ){
                    printf("Found it\n");
                    return *start;
                }
            }
        }
        p ++;
    }
    printf("Not Found\n");
    return 0;
}

As soon as it hits the w it prematurely exit out of the loop. I am not sure why its doing this? This is my question.

Output:

p: h compares with q: w

p: e compares with q: w

p: l compares with q: w

p: l compares with q: w

p: o compares with q: w

p: compares with q: w

Not Found


Edit:

gcc -Wall filename.c Compiler is not giving me any kinds of warnings/errors.

2 Answers 2

2

Your for loop is comparing i to the length of the string p points to, but since you keep changing p, that length keeps getting smaller, which is why the for loop ends about halfway through checking p.

If instead of using a counter i (which you don't do anything else with), you could instead replace the for loop with while ( *p != '\0' ): loop until you reach the end of p.

(Note: you can actually stop that loop as soon as then length of what is left is less than the length of the string q points to, but I'll leave that as an exercise.)

Sign up to request clarification or add additional context in comments.

Comments

0

I believe this would work:

char* find_pos_in_string(char* string, char* find) {
    for (char* s = string; *s != '\0'; ++s) {
        char* f = find;
        for (char* s_tmp = s; (*f == *s_tmp) && (*s_tmp != '\0') && (*f != '\0'); ++f, ++s_tmp);
        if (*f == '\0') return s;
    }
    return NULL;
}

The first for loops through all possible starting positions in string. Second for compares each character from the two strings starting at s. It stops if it finds some character difference or if either string reaches its end. If the find string has reached its end, it means that all subsequent character match: we have found our position (i.e., s). If the outer loop finish without a match, find doesn't exist as a substring. We return NULL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.