1

This is what I have so far:

for(i = 0; i <= 9; i++){
    printf("%d", i);
    found = strpbrk(nameholder[i], searchterm);
    if(strpbrk(nameholder[i], searchterm) == searchterm){
        printf("found\n");
        foundwhere = i + 1;
        break;
    }
}// end for

When I run the program, the strpbrk function finds the string, but for some reason it never triggers the if statement. What am I missing?

3
  • What is your test input? Commented Sep 18, 2012 at 16:59
  • You're using the wrong operator. == is for numerical equality. Commented Sep 18, 2012 at 17:53
  • I got it working it was a combination of different things, using strcmp() instead of strpbrk() was the first step in figuring it out though. When i used strcmp() it would return 1 as the result, so then i realize an extra character was being added to the original string input. So thanks for the help! Commented Sep 18, 2012 at 18:17

2 Answers 2

2

According to http://en.cppreference.com/w/c/string/byte/strpbrk , strpbrk() is for

const char* strpbrk( const char* dest, const char* str );
Finds the first character in byte string pointed to by dest, that is also in byte string pointed to by str.

Thus, if you really want to find the whole searchterm instead of any character of searchterm in nameholder[i], you should use strcmp or strstr.

Also note that the operator == can not be used to compare the equality of two char* strings since it simply compare if the addresses are equal or not disregarding the string content. Use strcmp() instead.

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

1 Comment

I got it working it was a combination of different things, using strcmp() instead of strpbrk() was the first step in figuring it out though. When i used strcmp() it would return 1 as the result, so then i realize an extra character was being added to the original string input. So thanks for the help!
-1

If I correctly understood (your description is vague) what you are trying to do, then you seem to be using a wrong function.

Quoting cpp docs on strpbrk:

Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.

That's not what you want it to do, right? You should be looking at strcpm function. http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

Your code should look like:

for(i = 0; i <= 9; i++){ 
    if(strcmp(nameholder[i], searchterm) == 0){
        printf("found\n");
        foundwhere = i + 1;
        break;
    }
}// end for

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.