need some help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ash

    need some help

    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.

    -- i tried to solve it but it seems that i am not understanding this
    question at all.i am taking this question as:

    1st string- "cat"
    2nd string-"i like cat."

    i have to return pointer that has strarting address of 'c' of cat of
    2nd string.i can make a program ( that finds a string into another )
    but how to return pointer of that string i don`t know.
    please suggest some advice.

    2) write a 'C' function
    char **readAndcreate (int n)
    the function reads "n" strings from the input and creates a list of
    such strings dynamically using "malloc" library call.
    -- as well as i understand i have to make a linked list that will hold
    the strings(i can make it )but one thing i didn`t understand what this
    function will return, Address of first node of that list or something
    else.if yes please describe how?

    my english is not good but i think you have understood my problems.if
    anyone can suggest a good link related to my problems, i will be
    thankful.
    thankx in advance
    :)

  • Richard Heathfield

    #2
    Re: need some help

    ash said:
    [color=blue]
    > 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.[/color]

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

    #include <string.h>
    char *ashstrstr(char *haystack, char *needle)
    {
    return strstr(haystack , needle);
    }
    [color=blue]
    > 2) write a 'C' function
    > char **readAndcreate (int n)
    > the function reads "n" strings from the input and creates a list of
    > such strings dynamically using "malloc" library call.
    > -- as well as i understand i have to make a linked list that will hold
    > the strings(i can make it )but one thing i didn`t understand what this
    > function will return, Address of first node of that list or something
    > else.if yes please describe how?[/color]

    If it were asking for a linked list, it would have said so, and there would
    have been some kind of linked list thingy in the prototype, and it didn't
    and there isn't so it isn't.

    No, what it's looking for is this:

    1) set up your array of n char * objects, like this:

    char **new = malloc(n * sizeof *new);
    if(new != NULL)
    {

    2) the next stage is to write (or find) a function that can read an entire
    line from standard input, reallocating storage as and when necessary to
    ensure that there is sufficient room to store the string. For a very simple
    way to do this that will suit you very well, look for Chuck Falconer's
    ggets() function which, last I heard, could be found at:

    <http://cbfalconer.home .att.net/download/ggets.zip>

    3) simply call this function in a loop, assigning each pointer thus obtained
    to new[i], where i is your loop counter, running from 0 to n-1. What will
    you do if the function returns NULL, to indicate insufficient storage, or
    perhaps an absence of input data?

    4) return new;


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • Fred Kleinschmidt

      #3
      Re: need some help


      "Richard Heathfield" <invalid@invali d.invalid> wrote in message
      news:dcqdncLBzs urHBTZRVnytQ@bt .com...[color=blue]
      > ash said:
      >[color=green]
      >> 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.[/color]
      >
      > Well, that one's easy, at any rate.
      >
      > #include <string.h>
      > char *ashstrstr(char *haystack, char *needle)
      > {
      > return strstr(haystack , needle);
      > }[/color]

      This is wrong. It should be strstr(needle, haystack).
      The instruction was to find first occurence of s1 in s2.
      strstr finds first s2 in s1.

      For this homework, I think the instructor really wants
      the student to write the internals of strstr. If so, he/she
      should have explicitly stated that using strstr was not allowed.
      [color=blue]
      >[color=green]
      >> 2) write a 'C' function
      >> char **readAndcreate (int n)
      >> the function reads "n" strings from the input and creates a list of
      >> such strings dynamically using "malloc" library call.
      >> -- as well as i understand i have to make a linked list that will hold
      >> the strings(i can make it )but one thing i didn`t understand what this
      >> function will return, Address of first node of that list or something
      >> else.if yes please describe how?[/color]
      >
      > If it were asking for a linked list, it would have said so, and there
      > would
      > have been some kind of linked list thingy in the prototype, and it didn't
      > and there isn't so it isn't.
      >
      > No, what it's looking for is this:
      >
      > 1) set up your array of n char * objects, like this:
      >
      > char **new = malloc(n * sizeof *new);
      > if(new != NULL)
      > {
      >
      > 2) the next stage is to write (or find) a function that can read an entire
      > line from standard input, reallocating storage as and when necessary to
      > ensure that there is sufficient room to store the string. For a very
      > simple
      > way to do this that will suit you very well, look for Chuck Falconer's
      > ggets() function which, last I heard, could be found at:
      >
      > <http://cbfalconer.home .att.net/download/ggets.zip>
      >
      > 3) simply call this function in a loop, assigning each pointer thus
      > obtained
      > to new[i], where i is your loop counter, running from 0 to n-1. What will
      > you do if the function returns NULL, to indicate insufficient storage, or
      > perhaps an absence of input data?
      >
      > 4) return new;
      >
      >
      > --
      > 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)[/color]
      --
      Fred L. Kleinschmidt
      Boeing Associate Technical Fellow
      Technical Architect, Software Reuse Project


      Comment

      • osmium

        #4
        Re: need some help

        "Richard Heathfield" wrote:
        [color=blue]
        > ash said:
        >[color=green]
        >> 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.[/color]
        >
        > Well, that one's easy, at any rate.
        >
        > #include <string.h>
        > char *ashstrstr(char *haystack, char *needle)
        > {
        > return strstr(haystack , needle);
        > }[/color]

        You know perfectly well that that is not what the instructor wanted.


        Comment

        • Richard Heathfield

          #5
          Re: need some help

          Fred Kleinschmidt said:
          [color=blue]
          >
          > "Richard Heathfield" <invalid@invali d.invalid> wrote in message
          > news:dcqdncLBzs urHBTZRVnytQ@bt .com...[color=green]
          >> ash said:
          >>[color=darkred]
          >>> 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.[/color]
          >>
          >> Well, that one's easy, at any rate.
          >>
          >> #include <string.h>
          >> char *ashstrstr(char *haystack, char *needle)
          >> {
          >> return strstr(haystack , needle);
          >> }[/color]
          >
          > This is wrong. It should be strstr(needle, haystack).[/color]

          <sigh> The easy ones are always hardest.
          [color=blue]
          > The instruction was to find first occurence of s1 in s2.
          > strstr finds first s2 in s1.[/color]

          Quite right. My apologies.
          [color=blue]
          > For this homework, I think the instructor really wants
          > the student to write the internals of strstr.[/color]

          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...
          [color=blue]
          > If so, he/she
          > should have explicitly stated that using strstr was not allowed.[/color]

          Precisely so.

          <snip>

          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at above domain (but drop the www, obviously)

          Comment

          • ash

            #6
            Re: need some help


            one friend advised me to use "strstr" function, this is a easy way to
            solve that question by use built in function but actually i was trying
            to make this function and i want help in writing that function.

            Comment

            • ash

              #7
              Re: need some help


              one friend advised me to use "strstr" function, this is a easy way to
              solve that question by use built in function but actually i was trying
              to make this function and i want help in writing that function.

              Comment

              • Default User

                #8
                Re: need some help

                ash wrote:
                [color=blue]
                >
                > one friend advised me to use "strstr" function[/color]

                That's nice. See below (it's still not clear to me which Google sites
                are "fixed" and which aren't).



                Brian

                --
                Please quote enough of the previous message for context. To do so from
                Google, click "show options" and use the Reply shown in the expanded
                header.

                Comment

                • CBFalconer

                  #9
                  Re: need some help

                  Richard Heathfield wrote:[color=blue]
                  >[/color]
                  .... snip ...[color=blue]
                  >
                  > 2) the next stage is to write (or find) a function that can read
                  > an entire line from standard input, reallocating storage as and
                  > when necessary to ensure that there is sufficient room to store
                  > the string. For a very simple way to do this that will suit you
                  > very well, look for Chuck Falconer's ggets() function which,
                  > last I heard, could be found at:
                  >
                  > <http://cbfalconer.home .att.net/download/ggets.zip>
                  >
                  > 3) simply call this function in a loop, assigning each pointer
                  > thus obtained to new[i], where i is your loop counter, running
                  > from 0 to n-1. What will you do if the function returns NULL, to
                  > indicate insufficient storage, or perhaps an absence of input data?[/color]

                  int ggets(char**) returns 0 for success, EOF for eof, and positive
                  for lack of memory. So a suitable read'em'all loop is:

                  while (0 == ggets(&buffptr) ) { ... }

                  --
                  Some informative links:
                  news:news.annou nce.newusers
                  Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!





                  Comment

                  • Richard Heathfield

                    #10
                    Re: need some help

                    CBFalconer said:
                    [color=blue]
                    > Richard Heathfield wrote:[color=green]
                    >>[/color]
                    > ... snip ...[color=green]
                    >>
                    >> For a very simple way to do this that will suit you
                    >> very well, look for Chuck Falconer's ggets() function which,
                    >> last I heard, could be found at:
                    >>
                    >> <http://cbfalconer.home .att.net/download/ggets.zip>
                    >>
                    >> 3) simply call this function in a loop, assigning each pointer
                    >> thus obtained to new[i], where i is your loop counter, running
                    >> from 0 to n-1. What will you do if the function returns NULL, to
                    >> indicate insufficient storage, or perhaps an absence of input data?[/color]
                    >
                    > int ggets(char**) returns 0 for success, EOF for eof, and positive
                    > for lack of memory. So a suitable read'em'all loop is:
                    >
                    > while (0 == ggets(&buffptr) ) { ... }[/color]

                    Ah, I had forgotten that. Thank you, Chuck.

                    --
                    Richard Heathfield
                    "Usenet is a strange place" - dmr 29/7/1999

                    email: rjh at above domain (but drop the www, obviously)

                    Comment

                    • shailesh.budhaner@gmail.com

                      #11
                      Re: need some help

                      Hi
                      u can use strstr function which returns int telling position of first
                      occurance of one string in other string
                      by using that position(index) u can return poiter.
                      ash wrote:[color=blue]
                      > 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.
                      >
                      > -- i tried to solve it but it seems that i am not understanding this
                      > question at all.i am taking this question as:
                      >
                      > 1st string- "cat"
                      > 2nd string-"i like cat."
                      >
                      > i have to return pointer that has strarting address of 'c' of cat of
                      > 2nd string.i can make a program ( that finds a string into another )
                      > but how to return pointer of that string i don`t know.
                      > please suggest some advice.
                      >
                      > 2) write a 'C' function
                      > char **readAndcreate (int n)
                      > the function reads "n" strings from the input and creates a list of
                      > such strings dynamically using "malloc" library call.
                      > -- as well as i understand i have to make a linked list that will hold
                      > the strings(i can make it )but one thing i didn`t understand what this
                      > function will return, Address of first node of that list or something
                      > else.if yes please describe how?
                      >
                      > my english is not good but i think you have understood my problems.if
                      > anyone can suggest a good link related to my problems, i will be
                      > thankful.
                      > thankx in advance
                      > :)[/color]

                      Comment

                      • shailesh.budhaner@gmail.com

                        #12
                        Re: need some help

                        Hi
                        u can use strstr function which returns int telling position of first
                        occurance of one string in other string
                        by using that position(index) u can return poiter.
                        ash wrote:[color=blue]
                        > 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.
                        >
                        > -- i tried to solve it but it seems that i am not understanding this
                        > question at all.i am taking this question as:
                        >
                        > 1st string- "cat"
                        > 2nd string-"i like cat."
                        >
                        > i have to return pointer that has strarting address of 'c' of cat of
                        > 2nd string.i can make a program ( that finds a string into another )
                        > but how to return pointer of that string i don`t know.
                        > please suggest some advice.
                        >
                        > 2) write a 'C' function
                        > char **readAndcreate (int n)
                        > the function reads "n" strings from the input and creates a list of
                        > such strings dynamically using "malloc" library call.
                        > -- as well as i understand i have to make a linked list that will hold
                        > the strings(i can make it )but one thing i didn`t understand what this
                        > function will return, Address of first node of that list or something
                        > else.if yes please describe how?
                        >
                        > my english is not good but i think you have understood my problems.if
                        > anyone can suggest a good link related to my problems, i will be
                        > thankful.
                        > thankx in advance
                        > :)[/color]

                        Comment

                        • Malcolm

                          #13
                          Re: need some help




                          "ash" <ashishmourya21 @rediffmail.com > wrote[color=blue]
                          >
                          > one friend advised me to use "strstr" function, this is a easy way to
                          > solve that question by use built in function but actually i was trying
                          > to make this function and i want help in writing that function.
                          >[/color]
                          First write the skeleton

                          /*
                          mystrstr - find the first occurrence of substring in string
                          Params: substring - string to search for
                          string - string to search in.
                          Returns: pointer to occurence of substring in string, NULL if none found.
                          */
                          char *mystrstr(char *substring, char *string)
                          {
                          }

                          Now for the algorithm.

                          Step through string until you find a character that matches the first
                          character of substring.
                          If you reach the end of the string, return NULL.
                          Now step through substring comparing every character to the following
                          characters in string. If you get a mismatch, abort and continue stepping
                          through string.
                          If you get to the NUL at the end of substring, you have found a perfect
                          match. Return the pointer to the start of the substring.

                          Here's a test funtion.

                          int main(void)
                          {
                          char *sub;

                          sub = mystrstr("Fred" , "My name is Fred and I am dead");
                          /* should print out "Fred and I am dead"
                          printf("%s\n", sub);
                          sub = mystrstr("Frede rick", "My name is Fred and I am dead"):
                          /* should print out that sub is null */
                          printf("%p\n", sub);
                          sub = mystrstr("Frede rick", "Fred");
                          /* should print out that sub is null */
                          printf("%p\n", sub);
                          sub = mystrstr("Fred" , "Fred, is Fred dead?");
                          /* should print out "Fred, is Fred dead?" */
                          printf("%s\n", sub);
                          sub = mystrstr("Fred" , sub);
                          /* should print out "Fred dead?");
                          printf("%s\n", sub);
                          return 0;
                          }

                          --
                          Buy my book 12 Common Atheist Arguments (refuted)
                          $1.25 download or $7.20 paper, available www.lulu.com/bgy1mm


                          Comment

                          • Michael Mair

                            #14
                            Re: need some help

                            Malcolm schrieb:[color=blue]
                            > "ash" <ashishmourya21 @rediffmail.com > wrote
                            >[color=green]
                            >>one friend advised me to use "strstr" function, this is a easy way to
                            >>solve that question by use built in function but actually i was trying
                            >>to make this function and i want help in writing that function.
                            >>[/color]
                            >
                            > First write the skeleton
                            >
                            > /*
                            > mystrstr - find the first occurrence of substring in string
                            > Params: substring - string to search for
                            > string - string to search in.
                            > Returns: pointer to occurence of substring in string, NULL if none found.
                            > */
                            > char *mystrstr(char *substring, char *string)
                            > {
                            > }
                            >
                            > Now for the algorithm.
                            >
                            > Step through string until you find a character that matches the first
                            > character of substring.
                            > If you reach the end of the string, return NULL.
                            > Now step through substring comparing every character to the following
                            > characters in string. If you get a mismatch, abort and continue stepping
                            > through string.
                            > If you get to the NUL at the end of substring, you have found a perfect
                            > match. Return the pointer to the start of the substring.
                            >
                            > Here's a test funtion.
                            >
                            > int main(void)
                            > {
                            > char *sub;
                            >
                            > sub = mystrstr("Fred" , "My name is Fred and I am dead");
                            > /* should print out "Fred and I am dead"[/color]

                            ITYM
                            /* should print out "Fred and I am dead" */
                            [color=blue]
                            > printf("%s\n", sub);[/color]

                            No prototype in scope. #include <stdio.h>
                            [color=blue]
                            > sub = mystrstr("Frede rick", "My name is Fred and I am dead"):[/color]
                            ^
                            ;
                            [color=blue]
                            > /* should print out that sub is null */
                            > printf("%p\n", sub);
                            > sub = mystrstr("Frede rick", "Fred");
                            > /* should print out that sub is null */
                            > printf("%p\n", sub);
                            > sub = mystrstr("Fred" , "Fred, is Fred dead?");
                            > /* should print out "Fred, is Fred dead?" */
                            > printf("%s\n", sub);
                            > sub = mystrstr("Fred" , sub);[/color]

                            ITYM:
                            sub = mystrstr("Fred" , sub+1);
                            This is the thing I originally wanted to remark but I
                            foundthe other stuff along the way...
                            [color=blue]
                            > /* should print out "Fred dead?");[/color]

                            Once again: Missing */ -- in this case an error.
                            [color=blue]
                            > printf("%s\n", sub);
                            > return 0;
                            > }[/color]

                            Cheers
                            Michael
                            --
                            E-Mail: Mine is an /at/ gmx /dot/ de address.

                            Comment

                            • osmium

                              #15
                              Re: need some help

                              "ash" writes:
                              [color=blue]
                              > one friend advised me to use "strstr" function, this is a easy way to
                              > solve that question by use built in function but actually i was trying
                              > to make this function and i want help in writing that function.[/color]

                              Your friend is what we in America call a "smartass". It is not always a good
                              idea to try to show the instructor that you are more clever than he is.

                              You "friends" proposal answers the question: "Write a function that calls a
                              function that takes two strings and .....".
                              That is not what you were told to do.


                              Comment

                              Working...