* strstr and strchr
@ 2009-02-17 8:30 Fundu
2009-02-17 10:48 ` Bert Wesarg
0 siblings, 1 reply; 5+ messages in thread
From: Fundu @ 2009-02-17 8:30 UTC (permalink / raw)
To: linux-c-programming
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 !
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: strstr and strchr
2009-02-17 8:30 strstr and strchr Fundu
@ 2009-02-17 10:48 ` Bert Wesarg
2009-02-17 11:52 ` Fundu
0 siblings, 1 reply; 5+ messages in thread
From: Bert Wesarg @ 2009-02-17 10:48 UTC (permalink / raw)
To: fundu_1999; +Cc: linux-c-programming
On Tue, Feb 17, 2009 at 09:30, Fundu <fundu_1999@yahoo.com> wrote:
> 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, '\'')
> so a printf("[%s]\n", strchr(ptr, "'"));
> would print [ lastName='Smith'/>]
>
> which doesn't make sense to me.
>
> can someone shed some light. TIA !
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: strstr and strchr
2009-02-17 10:48 ` Bert Wesarg
@ 2009-02-17 11:52 ` Fundu
2009-02-17 11:58 ` Bert Wesarg
0 siblings, 1 reply; 5+ messages in thread
From: Fundu @ 2009-02-17 11:52 UTC (permalink / raw)
To: Bert Wesarg; +Cc: linux-c-programming
Bert,
i did try that but got same response.
--- On Tue, 2/17/09, Bert Wesarg <bert.wesarg@googlemail.com> wrote:
> From: Bert Wesarg <bert.wesarg@googlemail.com>
> Subject: Re: strstr and strchr
> To: fundu_1999@yahoo.com
> Cc: linux-c-programming@vger.kernel.org
> Date: Tuesday, February 17, 2009, 2:48 AM
> On Tue, Feb 17, 2009 at 09:30, Fundu
> <fundu_1999@yahoo.com> wrote:
> > 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, '\'')
>
> > so a printf("[%s]\n", strchr(ptr,
> "'"));
> > 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 majordomo@vger.kernel.org
> More majordomo info at
> http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: strstr and strchr
2009-02-17 11:52 ` Fundu
@ 2009-02-17 11:58 ` Bert Wesarg
2009-02-17 12:00 ` Bert Wesarg
0 siblings, 1 reply; 5+ messages in thread
From: Bert Wesarg @ 2009-02-17 11:58 UTC (permalink / raw)
To: fundu_1999; +Cc: linux-c-programming
On Tue, Feb 17, 2009 at 12:52, Fundu <fundu_1999@yahoo.com> wrote:
> 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' />
^
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: strstr and strchr
2009-02-17 11:58 ` Bert Wesarg
@ 2009-02-17 12:00 ` Bert Wesarg
0 siblings, 0 replies; 5+ messages in thread
From: Bert Wesarg @ 2009-02-17 12:00 UTC (permalink / raw)
To: fundu_1999; +Cc: linux-c-programming
On Tue, Feb 17, 2009 at 12:58, Bert Wesarg <bert.wesarg@googlemail.com> wrote:
> On Tue, Feb 17, 2009 at 12:52, Fundu <fundu_1999@yahoo.com> wrote:
>> 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' />
> ^
This looks probably wrong, because of malformed whitespace.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-02-17 12:00 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-17 8:30 strstr and strchr Fundu
2009-02-17 10:48 ` Bert Wesarg
2009-02-17 11:52 ` Fundu
2009-02-17 11:58 ` Bert Wesarg
2009-02-17 12:00 ` Bert Wesarg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).