0

I have a problem, I need to test if a string is present in an other array in C using pointer. I tried this, but it doesn't work, if anyone has any suggestion... here is the code I tried, thank you in advance...

/* Like the strstr() function. It returns a pointer to the first occurrence of the string aiguille in the string meule_de_foin.
 * @param meule_de_foin the string to search in
 * @param aiguille the string to find
 * @return a pointer to the first occurrence of the string aiguille in the string meule_de_foin if aiguille is in meule_de_foin, NULL otherwise
 */

const char * IMPLEMENT(indexOfString)(const char *meule_de_foin, const char *aiguille) {
    int isFound; isFound=0;
    int first; first=meule_de_foin;

    while(isFound==0){
        if(*aiguille=='\0' && *meule_de_foin=='\0'){
            isFound=1;
        } else if (*aiguille == *meule_de_foin){
            aiguille=aiguille+1;
            meule_de_foin=meule_de_foin+1;
        }else{
            isFound=2;
        }
    }

    if(isFound==1){
        return (first);
    }else{
        return(NULL);
    }
}

if(isFound==1){
    return (first);
}else{
    return(NULL);
}
8
  • 2
    It's a lot easier when the code is actually in English. Commented Nov 16, 2017 at 7:32
  • 1
    Your function only checks if aiguille is found at the beginning of meule_de_foin. Commented Nov 16, 2017 at 7:34
  • 1
    first should be const char *, not int. Commented Nov 16, 2017 at 7:36
  • 1
    Is IMPLEMENT a macro? Please remove the extra lines at the end of the snippet. Commented Nov 16, 2017 at 7:39
  • @Ante depends who reads the code :-) Btw "aiguille" is needle, and "meule de foin" is haystack Commented Nov 16, 2017 at 7:59

1 Answer 1

5

You're only testing if two strings are completely equal.

You need to stop checking when you reach the end of the search string, even if you're not at the end of the string to search.

And if it's not found, you need to check again starting at the next character, and keep repeating this until you reach the end of the string. So you need another loop around the loop that searches.

int isFound = 0;
const char *first;
for (first = meule_de_foin; *first != '\0' && isFound != 1; first++) {
    isFound = 0;
    const char *search = aiguille;
    const char *cur = first;
    while (!isFound) {
        if (*search == '\0') { // End of search string
            isFound = 1;
        } else if (*search != *cur) { // Non-matching character, stop matching
            isFound = 2;
        } else { // Keep matching
            search++;
            cur++;
        }
    }
}

if (isFound == 1) {
    return first;
else {
    return NULL;
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much for your answer! I totally get my mistake and how you corrected it, I have a question tho, what does the "true" means in the while loop?
It just means that the loop keeps repeating forever. Except that the break statements inside will end it.
But I changed it back to your way of testing isFound.

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.