tags:

views:

198

answers:

3

I was wondering how could I match the string "Just" in str1 if str1 contains the strings as:

"this is Just/1.1.249.4021 a test" 
// "Just" will always be the same

I'm trying to match it using strstr but so far it won't match because of the /...

Any suggestions on how to match it? Thanks

+1  A: 

This works for me - what about you?

#include <string.h>
#include <stdio.h>
int main(void)
{
    char haystack[] = "this is just\2323 a test";
    char needle[] = "just";
    char *loc = strstr(haystack, needle);
    if (loc == 0)
        printf("Did not find <<%s>> in <<%s>>\n", needle, haystack);
    else
        printf("Found <<%s>> in <<%s> at <<%s>>\n", needle, haystack, loc);
    return(0);
}
Jonathan Leffler
Sorry, the string should be as: Just/1.1.249.4021
David78
The C++ standard says that string literals are of type "array of const char", thus it is recommended to declare them as const char*
rep_movsd
@rep_movsd: yes, but I'm not declaring any string literals. I'm declaring two variable arrays with convenient initializers. Granted, I could make them const; there is absolutely no obligation to do so, and I could modify the strings (carefully) if I wanted to.
Jonathan Leffler
A: 

Something must be wrong with how you use strstr() The following code works just fine...

const char *s = "this is just\2323 a test";
char *p = strstr(s, "just");
if(p)
    printf("Found 'just' at index %d\n", (int)(p - s));
rep_movsd
A: 

If the string is actually "Just/1.1.249.4021" then it would fail to find "just", because strstr is case-sensitive. If you need a case-insensitive version you have to write your own or Google for an existing implementation.

Evgeny