Skip to contentSkip to conversation

strstr and strchr

DISCUSSION Fundu 2 participants 5 messages Feb 17, 2009

lore
Share & tools
lore

Fundu

author
#0 lore raw

looks strange to me
if i look for a str using strstr it gives me the starting index of the string. so let say if i'm looking for "firstName" in "<Info firstName=\"sam\" lastName=\"Smith\" />" using strstr(ptr, "firstName"); i would get the pointer pointing to the f in firstName.

whereas if i use strchr and using a slight variation of the string mentioned above(suing ' instead of ") "<Info firstName='sam' lastName='Smith'/>" strchr(ptr, "'") returns a pointer pointing after the '
so a printf("[%s]\n", strchr(ptr, "'"));
would print [ lastName='Smith'/>]

which doesn't make sense to me.

can someone shed some light. TIA !

#1 lore raw
Quoted 7 lines · Fundu

looks strange to me
if i look for a str using strstr it gives me the starting index of the string. so let say if i'm looking for "firstName" in "<Info firstName=\"sam\" lastName=\"Smith\" />" using strstr(ptr, "firstName"); i would get the pointer pointing to the f in firstName.

whereas if i use strchr and using a slight variation of the string mentioned above(suing ' instead of ") "<Info firstName='sam' lastName='Smith'/>" strchr(ptr, "'") returns a pointer pointing after the '

strchr() takes a int as second argument, not a 'char *', I suggest trying:

strchr(ptr, '\'')

Quoted 6 lines

so a printf("[%s]\n", strchr(ptr, "'"));
would print [ lastName='Smith'/>]

which doesn't make sense to me.

can someone shed some light. TIA !

author
#2 lore raw

Bert,
i did try that but got same response.

Quoted 41 lines · Bert Wesarg

From: Bert Wesarg <[email protected]>
Subject: Re: strstr and strchr
To: [email protected]
Cc: [email protected]
Date: Tuesday, February 17, 2009, 2:48 AM

Quoted 2 lines · Fundu

looks strange to me
if i look for a str using strstr it gives me the

starting index of the string. so let say if i'm looking for "firstName" in "<Info
firstName=\"sam\"
lastName=\"Smith\" />" using

Quoted 1 line

strstr(ptr, "firstName"); i would get the

pointer pointing to the f in firstName.

Quoted 3 lines

whereas if i use strchr and using a slight variation

of the string mentioned above(suing ' instead of ")
"<Info firstName='sam'
lastName='Smith'/>"

Quoted 1 line

strchr(ptr, "'") returns a pointer

pointing after the '
strchr() takes a int as second argument, not a 'char *', I suggest trying:

strchr(ptr, '\'')

Quoted 1 line

so a printf("[%s]\n", strchr(ptr,

"'"));

Quoted 5 lines

would print [ lastName='Smith'/>]

which doesn't make sense to me.

can someone shed some light. TIA !

--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 2 lines · Fundu

Bert,
i did try that but got same response.

I'm not:

int
main(int ac, char *av[])

{
    char *str = "<Info firstName='sam' lastName='Smith' />";
    char *ptr = strchr(str, '\'');

    if (!ptr)
        return 1;

    printf("%s\n", str);
    printf("%*s^\n", (int)(ptr - str), "");

    return 0;
}

prints:

<Info firstName='sam' lastName='Smith' />

                ^
#4 lore raw
Quoted 20 lines · Bert Wesarg
Quoted 2 lines · Fundu

Bert,
i did try that but got same response.

I'm not:

int
main(int ac, char *av[])

{
   char *str = "<Info firstName='sam' lastName='Smith' />";
   char *ptr = strchr(str, '\'');

   if (!ptr)
       return 1;

   printf("%s\n", str);
   printf("%*s^\n", (int)(ptr - str), "");

   return 0;
}
Quoted 4 lines

prints:

<Info firstName='sam' lastName='Smith' />

               ^

This looks probably wrong, because of malformed whitespace.