string splitting

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

    #1

    string splitting

    I want to write a program that:

    char * strplit(char* str1, char *str2, char * stroriginal,int
    split_point)

    that take stroriginal and split in the split_point element of the
    string the string into two other strings,

    example:

    "Hello World"
    i want to split this string into "Hello " "World"



    thanks in advance,
    andrea

  • Richard Heathfield

    #2
    Re: string splitting

    Andrea said:
    I want to write a program that:
    >
    char * strplit(char* str1, char *str2, char * stroriginal,int
    split_point)
    >
    that take stroriginal and split in the split_point element of the
    string the string into two other strings,
    >
    example:
    >
    "Hello World"
    i want to split this string into "Hello " "World"
    What's stopping you? What have you tried? (And with whom lies the
    responsibility for storage allocation - the caller or the splitter?)

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

    email: rjh at the above domain, - www.

    Comment

    • Ian Malone

      #3
      Re: string splitting

      Richard Heathfield wrote:
      Andrea said:
      >
      >I want to write a program that:
      >>
      >char * strplit(char* str1, char *str2, char * stroriginal,int
      >split_point)
      >>
      >that take stroriginal and split in the split_point element of the
      >string the string into two other strings,
      >>
      >example:
      >>
      >"Hello World"
      >i want to split this string into "Hello " "World"
      >
      What's stopping you?
      An inappropriate prototype?
      char * strplit(char* str1, char *str2, char * stroriginal,
      int split_point)

      Questions for the OP:
      What should be passed in str1, str2, stroriginal and
      split_point? What is the significance of the return
      value going to be?

      As written I suspect that you plan to put the two sub-
      strings into the memory pointed to by str1 and str2,
      how will your function know that the objects pointed
      to by these are large enough to hold the sub-strings?

      --
      imalone

      Comment

      • santosh

        #4
        Re: string splitting


        Andrea wrote:
        I want to write a program that:
        >
        char * strplit(char* str1, char *str2, char * stroriginal,int
        split_point)
        >
        that take stroriginal and split in the split_point element of the
        string the string into two other strings,
        >
        example:
        >
        "Hello World"
        i want to split this string into "Hello " "World"
        Please post your attempt. Additionally what does the return value
        point to? It's also perhaps better to const qualify the pointer to the
        original string since your function need not modify it. You also need
        to decide whether memory allocation for str1 and str2 should be done
        in the caller or in the callee.

        Comment

        • Daniel Rudy

          #5
          Re: string splitting

          At about the time of 2/21/2007 3:52 AM, Andrea stated the following:
          I want to write a program that:
          >
          char * strplit(char* str1, char *str2, char * stroriginal,int
          split_point)
          >
          that take stroriginal and split in the split_point element of the
          string the string into two other strings,
          >
          example:
          >
          "Hello World"
          i want to split this string into "Hello " "World"
          >
          >
          >
          thanks in advance,
          andrea
          >
          Here's a hint: strsep.


          --
          Daniel Rudy

          Email address has been base64 encoded to reduce spam
          Decode email address using b64decode or uudecode -m

          Why geeks like computers: look chat date touch grep make unzip
          strip view finger mount fcsk more fcsk yes spray umount sleep

          Comment

          • Ben Pfaff

            #6
            Re: string splitting

            Daniel Rudy <spamthis@spamt his.netwrites:
            Here's a hint: strsep.
            strsep is not in the standard C library, so the OP may well not
            have it available to him.
            --
            "The lusers I know are so clueless, that if they were dipped in clue
            musk and dropped in the middle of pack of horny clues, on clue prom
            night during clue happy hour, they still couldn't get a clue."
            --Michael Girdwood, in the monastery

            Comment

            • Tydr Schnubbis

              #7
              Re: string splitting

              Ben Pfaff wrote:
              Daniel Rudy <spamthis@spamt his.netwrites:
              >
              >Here's a hint: strsep.
              >
              strsep is not in the standard C library, so the OP may well not
              have it available to him.
              strtok does basically the same thing. But this looks a bit like
              homework anyway.

              Comment

              • santosh

                #8
                Re: string splitting


                Daniel Rudy wrote:
                At about the time of 2/21/2007 3:52 AM, Andrea stated the following:
                I want to write a program that:

                char * strplit(char* str1, char *str2, char * stroriginal,int
                split_point)

                that take stroriginal and split in the split_point element of the
                string the string into two other strings,

                example:

                "Hello World"
                i want to split this string into "Hello " "World"
                >
                Here's a hint: strsep.
                The OP probably wants to implement the function himself as a learning
                exercise. Use an existing function would defeat that purpose.

                Comment

                • Richard Bos

                  #9
                  Re: string splitting

                  Tydr Schnubbis <fake@address.d udewrote:
                  Ben Pfaff wrote:
                  Daniel Rudy <spamthis@spamt his.netwrites:
                  Here's a hint: strsep.
                  strsep is not in the standard C library, so the OP may well not
                  have it available to him.
                  >
                  strtok does basically the same thing.
                  Almost. strtok won't split "hello world", but it will split an array
                  containing those characters.
                  But this looks a bit like homework anyway.
                  More than a bit.

                  Richard

                  Comment

                  • u plz

                    #10
                    Re: string splitting

                    On Wed, 21 Feb 2007 20:18:00 -0800, santosh wrote:
                    Daniel Rudy wrote:
                    >>
                    >Here's a hint: strsep.
                    >
                    The OP probably wants to implement the function himself as a learning
                    exercise. Use an existing function would defeat that purpose.
                    Also the fact that strsep in not defined in the C standard means that the
                    OP might not be able to use it anyway.

                    -Alok

                    Comment

                    • Daniel Rudy

                      #11
                      Re: string splitting

                      At about the time of 2/21/2007 1:09 PM, Ben Pfaff stated the following:
                      Daniel Rudy <spamthis@spamt his.netwrites:
                      >
                      >Here's a hint: strsep.
                      >
                      strsep is not in the standard C library, so the OP may well not
                      have it available to him.
                      It's not?


                      STRSEP(3) FreeBSD Library Functions Manual STRSEP(3)

                      NAME
                      strsep -- separate strings

                      LIBRARY
                      Standard C Library (libc, -lc)

                      SYNOPSIS
                      #include <string.h>

                      char *
                      strsep(char **stringp, const char *delim);

                      DESCRIPTION
                      The strsep() function locates, in the string referenced by *stringp, the
                      first occurrence of any character in the string delim (or the terminating
                      `\0' character) and replaces it with a `\0'. The location of the next
                      character after the delimiter character (or NULL, if the end of the
                      string was reached) is stored in *stringp. The original value of
                      *stringp is returned.

                      An ``empty'' field (i.e., a character in the string delim occurs as the
                      first character of *stringp) can be detected by comparing the location
                      referenced by the returned pointer to `\0'.

                      If *stringp is initially NULL, strsep() returns NULL.

                      EXAMPLES
                      The following uses strsep() to parse a string, containing tokens delim-
                      ited by white space, into an argument vector:

                      char **ap, *argv[10], *inputstring;

                      for (ap = argv; (*ap = strsep(&inputst ring, " \t")) != NULL;)
                      if (**ap != '\0')
                      if (++ap >= &argv[10])
                      break;

                      SEE ALSO
                      memchr(3), strchr(3), strcspn(3), strpbrk(3), strrchr(3), strspn(3),
                      strstr(3), strtok(3)

                      HISTORY
                      The strsep() function is intended as a replacement for the strtok() func-
                      tion. While the strtok() function should be preferred for portability
                      reasons (it conforms to ISO/IEC 9899:1990 (``ISO C90'')) it is unable to
                      handle empty fields, i.e., detect fields delimited by two adjacent delim-
                      iter characters, or to be used for more than a single string at a time.
                      The strsep() function first appeared in 4.4BSD.

                      FreeBSD 6.2 June 9, 1993 FreeBSD 6.2


                      I guess it's not, but it probably should be... 4.4BSD though is the
                      original version that FreeBSD is based on, so the function has been
                      around for a long time.


                      --
                      Daniel Rudy

                      Email address has been base64 encoded to reduce spam
                      Decode email address using b64decode or uudecode -m

                      Why geeks like computers: look chat date touch grep make unzip
                      strip view finger mount fcsk more fcsk yes spray umount sleep

                      Comment

                      • matevzb

                        #12
                        Re: string splitting

                        On Feb 23, 10:44 am, Daniel Rudy <spamt...@spamt his.netwrote:
                        At about the time of 2/21/2007 1:09 PM, Ben Pfaff stated the following:
                        Daniel Rudy <spamt...@spamt his.netwrites:
                        Here's a hint: strsep.
                        strsep is not in the standard C library, so the OP may well not
                        have it available to him.
                        >
                        It's not?
                        >
                        STRSEP(3) FreeBSD Library Functions Manual STRSEP(3)
                        >
                        NAME
                        strsep -- separate strings
                        >
                        LIBRARY
                        Standard C Library (libc, -lc)
                        This usually refers to libc, which may contain a lot more than ISO C
                        specifies (check strdup() for example). HP-UX man pages are really
                        useful in this area as they contain a "STANDARDS CONFORMANCE" section,
                        listing the standards that a function conforms to, e.g.:
                        strcoll(): AES, SVID3, XPG3, XPG4, ANSI C
                        strdup(): SVID2, SVID3
                        <snip>
                        HISTORY
                        The strsep() function is intended as a replacement for the strtok() func-
                        tion. While the strtok() function should be preferred for portability
                        reasons (it conforms to ISO/IEC 9899:1990 (``ISO C90'')) it is unable to
                        handle empty fields, i.e., detect fields delimited by two adjacent delim-
                        iter characters, or to be used for more than a single string at a time.
                        The strsep() function first appeared in 4.4BSD.
                        >
                        FreeBSD 6.2 June 9, 1993 FreeBSD 6.2
                        >
                        I guess it's not, but it probably should be... 4.4BSD though is the
                        original version that FreeBSD is based on, so the function has been
                        around for a long time.
                        It didn't even get into POSIX/SUS, why should it be in C?
                        --
                        WYCIWYG - what you C is what you get

                        Comment

                        • Richard Heathfield

                          #13
                          Re: string splitting

                          Daniel Rudy said:
                          At about the time of 2/21/2007 1:09 PM, Ben Pfaff stated the
                          following:
                          >Daniel Rudy <spamthis@spamt his.netwrites:
                          >>
                          >>Here's a hint: strsep.
                          >>
                          >strsep is not in the standard C library, so the OP may well not
                          >have it available to him.
                          >
                          It's not?
                          No.
                          STRSEP(3) FreeBSD Library Functions Manual
                          What makes you think FreeBSD get to decide what is in the standard C
                          library?

                          $ grep -i strsep c89.txt
                          $

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

                          email: rjh at the above domain, - www.

                          Comment

                          • Daniel Rudy

                            #14
                            Re: string splitting

                            At about the time of 2/23/2007 4:42 AM, Richard Heathfield stated the
                            following:
                            Daniel Rudy said:
                            >
                            >At about the time of 2/21/2007 1:09 PM, Ben Pfaff stated the
                            >following:
                            >>Daniel Rudy <spamthis@spamt his.netwrites:
                            >>>
                            >>>Here's a hint: strsep.
                            >>strsep is not in the standard C library, so the OP may well not
                            >>have it available to him.
                            >It's not?
                            >
                            No.
                            >
                            >STRSEP(3) FreeBSD Library Functions Manual
                            >
                            What makes you think FreeBSD get to decide what is in the standard C
                            library?
                            >
                            $ grep -i strsep c89.txt
                            $
                            >
                            I didn't say that FreeBSD should decide what goes into the C library.
                            What I was getting at is the fact that since the function has been
                            around along time, and that it's superior to strtok (strsep handles
                            situations that strtok doesn't), why hasn't it been included (at least
                            in the POSIX standard if not the C standard)?

                            Now I'm not too up on what functions comprise the standard C library and
                            what doesn't, reading THIS man page you can see the cause of confusion:

                            STRTOK(3) FreeBSD Library Functions Manual

                            NAME
                            strtok, strtok_r -- string tokens

                            LIBRARY
                            Standard C Library (libc, -lc)

                            SYNOPSIS
                            #include <string.h>

                            char *
                            strtok(char *str, const char *sep);

                            char *
                            strtok_r(char *str, const char *sep, char **last);

                            DESCRIPTION
                            This interface is obsoleted by strsep(3).

                            The strtok() function is used to isolate sequential tokens in a
                            null-terminated string, str. These tokens are separated in the


                            I cut it short because of its length. But in the bugs section,

                            BUGS
                            The System V strtok(), if handed a string containing only delimiter
                            char-
                            acters, will not alter the next starting point, so that a call to
                            strtok() with a different (or empty) delimiter string may return a
                            non-NULL value. Since this implementation always alters the next
                            start-
                            ing point, such a sequence of calls would always return NULL.


                            Oh well. Learn something new every day.


                            --
                            Daniel Rudy

                            Email address has been base64 encoded to reduce spam
                            Decode email address using b64decode or uudecode -m

                            Why geeks like computers: look chat date touch grep make unzip
                            strip view finger mount fcsk more fcsk yes spray umount sleep

                            Comment

                            • CBFalconer

                              #15
                              Re: string splitting

                              matevzb wrote:
                              >
                              .... snip ...
                              >
                              This usually refers to libc, which may contain a lot more than ISO
                              C specifies (check strdup() for example). HP-UX man pages are
                              really useful in this area as they contain a "STANDARDS
                              CONFORMANCE" section, listing the standards that a function
                              conforms to, e.g.: strcoll(): AES, SVID3, XPG3, XPG4, ANSI C
                              strdup(): SVID2, SVID3
                              <snip>
                              >HISTORY
                              > The strsep() function is intended as a replacement for the
                              > strtok() function. While the strtok() function should be
                              > preferred for portability reasons (it conforms to ISO/IEC
                              > 9899:1990 (``ISO C90'')) it is unable to handle empty fields,
                              > i.e., detect fields delimited by two adjacent delimiter
                              > characters, or to be used for more than a single string at a
                              > time. The strsep() function first appeared in 4.4BSD.
                              >>
                              >I guess it's not, but it probably should be... 4.4BSD though is the
                              >original version that FreeBSD is based on, so the function has been
                              >around for a long time.
                              >
                              It didn't even get into POSIX/SUS, why should it be in C?
                              You can try the following. The source code has been published here
                              before, if you want it search google.

                              /* ------- file toksplit.h ----------*/
                              #ifndef H_toksplit_h
                              # define H_toksplit_h

                              # ifdef __cplusplus
                              extern "C" {
                              # endif

                              #include <stddef.h>

                              /* copy over the next token from an input string, after
                              skipping leading blanks (or other whitespace?). The
                              token is terminated by the first appearance of tokchar,
                              or by the end of the source string.

                              The caller must supply sufficient space in token to
                              receive any token, Otherwise tokens will be truncated.

                              Returns: a pointer past the terminating tokchar.

                              This will happily return an infinity of empty tokens if
                              called with src pointing to the end of a string. Tokens
                              will never include a copy of tokchar.

                              released to Public Domain, by C.B. Falconer.
                              Published 2006-02-20. Attribution appreciated.
                              */

                              const char *toksplit(const char *src, /* Source of tokens */
                              char tokchar, /* token delimiting char */
                              char *token, /* receiver of parsed token */
                              size_t lgh); /* length token can receive */
                              /* not including final '\0' */

                              # ifdef __cplusplus
                              }
                              # endif
                              #endif
                              /* ------- end file toksplit.h ----------*/

                              --
                              Chuck F (cbfalconer at maineline dot net)
                              Available for consulting/temporary embedded and systems.
                              <http://cbfalconer.home .att.net>


                              Comment

                              Working...