strstr and strchr
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 !
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\" />" usingstrstr(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 !
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 AMQuoted 2 lines · Fundu
looks strange to me
if i look for a str using strstr it gives me thestarting index of the string. so let say if i'm looking for "firstName" in "<Info
firstName=\"sam\"
lastName=\"Smith\" />" usingQuoted 1 line
strstr(ptr, "firstName"); i would get thepointer 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 pointerpointing 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
Quoted 2 lines · Fundu
Bert,
i did try that but got same response.
I'm not:
intmain(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' />
^
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.