Re: need some help



Fred Kleinschmidt said:


"Richard Heathfield" <invalid@xxxxxxxxxxxxxxx> wrote in message
news:dcqdncLBzsurHBTZRVnytQ@xxxxxxxxx
ash said:

hi friends,
i have some questions whch is in my last year question papers.i need
some help to get logic of these questions.

1) write a C function, that takes two strings as arguments and returns
a pointer to the first occurrence of 1st string in 2nd string or NULL
if it is not present.

Well, that one's easy, at any rate.

#include <string.h>
char *ashstrstr(char *haystack, char *needle)
{
return strstr(haystack, needle);
}

This is wrong. It should be strstr(needle, haystack).

<sigh> The easy ones are always hardest.

The instruction was to find first occurence of s1 in s2.
strstr finds first s2 in s1.

Quite right. My apologies.

For this homework, I think the instructor really wants
the student to write the internals of strstr.

Yes, but here in comp.lang.c we try to get the OP to think, rather than
spoonfeed them a solution. In this case, I was trying to get him (or
possibly her - I haven't checked) to think about how to phrase the
question. Shame I messed up the answer, though...

If so, he/she
should have explicitly stated that using strstr was not allowed.

Precisely so.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.



Relevant Pages

  • Re: Implementing strstr
    ... that string doesn't appear at all). ... const char* strstr(const char* haystack, const char* needle) ...
    (comp.programming)
  • RE: Currency Field Format Correction
    ... Function SF_removeAllOnce(ByVal Haystack As String, ByVal Needle As String) ... Fieldname]) and run the query. ...
    (microsoft.public.access.modulesdaovba)
  • Re: PHP global namespace clogged up
    ... -string strchr(string haystack, string needle) ... int strpos(string haystack, string needel ): ...
    (comp.lang.php)
  • Documentation on strpos
    ... "Returns the numeric position of the first occurrence of needle in the ... full string as the needle parameter and the entire string will be used." ... I find the reference to "strrpos" confusing, ... haystack string." ...
    (comp.lang.php)
  • Re: [PHP] Fast prefix search?
    ... I'll probably use strstrto check whether a string starts with some ... So don't use strstr() use strpos. ... // it's a prefix. ... front of the haystack! ...
    (php.general)