How split 1 array in two

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ricardoramoscabral@gmail.com

    #1

    How split 1 array in two

    Hello to All.


    I have this array of strings: char str[90] = {" comp " Hello World
    " "};

    And i whant to split in two arrays from the original: ( { <-----
    str1 ------- "<---str2 -------->" }

    str1[10] = {comp}
    str2[80] = {Hello World}

    sorry for my english
  • Martin Ambuhl

    #2
    Re: How split 1 array in two

    ricardoramoscab ral@gmail.com wrote:
    Hello to All.
    >
    >
    I have this array of strings: char str[90] = {" comp " Hello World
    " "};
    char str[90] /* = { ... } */; is _not_ an array of strings. It is an
    array of char, which can contain a string,
    Your initializer has an odd number of '"' characters. Apart from the
    embedded end-of-line, it is for that reason malformed.

    And i whant to split in two arrays from the original: ( { <-----
    str1 ------- "<---str2 -------->" }
    >
    str1[10] = {comp}
    str2[80] = {Hello World}
    You might use strtok, but your current level of confusion suggests that
    you would not do so in anything like safety. Just find the beginning
    and end of each substring (functions like strstr, strchr, strlen can be
    useful) and copy them into your new strings with functions likw memmove,
    memcpy, or strncpy (strcpy is unlikely to handle any but the last the
    way you expect).

    Comment

    • ricardoramoscabral@gmail.com

      #3
      Re: How split 1 array in two

      int i=0;
      char str[] = "portugal\" a-programar";
      char delims[] = "\"";
      char *result = NULL;
      result = strtok( str, delims );
      while( result != NULL )
      {
      printf( "result is \"%s\"\n", result );
      result = strtok( NULL, delims );
      }

      Comment

      • ric

        #4
        Re: How split 1 array in two

        Hello Martin Ambuhl,


        With strtok works.

        int i=0;
        char str[] = "Teste\"a-programar";
        char delims[] = "\"";
        char *result = NULL;
        result = strtok( str, delims );
        while( result != NULL )
        {
        printf( "result is \"%s\"\n", result );
        result = strtok( NULL, delims );
        }


        I am trying to save the result of strtok in several separate arrays, but
        I am not succeed.

        We tried downloading string, using "if" with counter [i]

        Can you give some tips / help.

        Thanks.

        Comment

        • Thad Smith

          #5
          Re: How split 1 array in two

          ricardoramoscab ral@gmail.com wrote:
          I have this array of strings: char str[90] = {" comp " Hello World
          " "};
          That is not a valid statement in C and I don't know what you intended.
          Please post again with a valid declaration. char str[90] declares an
          array of char in C, not an array of strings.
          And i whant to split in two arrays from the original: ( { <-----
          str1 ------- "<---str2 -------->" }
          >
          str1[10] = {comp}
          str2[80] = {Hello World}
          Those are not valid C declarations, either. Clarify your problem.
          sorry for my english
          Your English is understandable.

          --
          Thad

          Comment

          • CBFalconer

            #6
            Re: How split 1 array in two

            ric wrote:
            >
            With strtok works.
            >
            .... snip ...
            >
            Can you give some tips / help.
            Try the following tknsplit routine. It is different from strtok,
            but I believe cleaner to use. For general use, compile it without
            defining TESTING.

            /* ------- file tknsplit.c ----------*/
            #include "tknsplit.h "

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

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

            Returns: a pointer past the terminating tknchar.

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

            A better name would be "strtkn", except that is reserved
            for the system namespace. Change to that at your risk.

            released to Public Domain, by C.B. Falconer.
            Published 2006-02-20. Attribution appreciated.
            Revised 2006-06-13 2007-05-26 (name)
            */

            const char *tknsplit(const char *src, /* Source of tkns */
            char tknchar, /* tkn delimiting char */
            char *tkn, /* receiver of parsed tkn */
            size_t lgh) /* length tkn can receive */
            /* not including final '\0' */
            {
            if (src) {
            while (' ' == *src) src++;

            while (*src && (tknchar != *src)) {
            if (lgh) {
            *tkn++ = *src;
            --lgh;
            }
            src++;
            }
            if (*src && (tknchar == *src)) src++;
            }
            *tkn = '\0';
            return src;
            } /* tknsplit */

            #ifdef TESTING
            #include <stdio.h>

            #define ABRsize 6 /* length of acceptable tkn abbreviations */

            /* ---------------- */

            static void showtkn(int i, char *tok)
            {
            putchar(i + '1'); putchar(':');
            puts(tok);
            } /* showtkn */

            /* ---------------- */

            int main(void)
            {
            char teststring[] = "This is a test, ,, abbrev, more";

            const char *t, *s = teststring;
            int i;
            char tkn[ABRsize + 1];

            puts(teststring );
            t = s;
            for (i = 0; i < 4; i++) {
            t = tknsplit(t, ',', tkn, ABRsize);
            showtkn(i, tkn);
            }

            puts("\nHow to detect 'no more tkns' while truncating");
            t = s; i = 0;
            while (*t) {
            t = tknsplit(t, ',', tkn, 3);
            showtkn(i, tkn);
            i++;
            }

            puts("\nUsing blanks as tkn delimiters");
            t = s; i = 0;
            while (*t) {
            t = tknsplit(t, ' ', tkn, ABRsize);
            showtkn(i, tkn);
            i++;
            }
            return 0;
            } /* main */

            #endif
            /* ------- end file tknsplit.c ----------*/


            /* ------- file tknsplit.h ----------*/
            #ifndef H_tknsplit_h
            # define H_tknsplit_h

            # ifdef __cplusplus
            extern "C" {
            # endif

            #include <stddef.h>

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

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

            Returns: a pointer past the terminating tknchar.

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

            released to Public Domain, by C.B. Falconer.
            Published 2006-02-20. Attribution appreciated.
            revised 2007-05-26 (name)
            */

            const char *tknsplit(const char *src, /* Source of tkns */
            char tknchar, /* tkn delimiting char */
            char *tkn, /* receiver of parsed tkn */
            size_t lgh); /* length tkn can receive */
            /* not including final '\0' */

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

            --
            Chuck F (cbfalconer at maineline dot net)
            <http://cbfalconer.home .att.net>
            Try the download section.



            --
            Posted via a free Usenet account from http://www.teranews.com

            Comment

            • James Fang

              #7
              Re: How split 1 array in two

              On Dec 2, 11:57 pm, ricardoramoscab ...@gmail.com wrote:
              Hello to All.
              >
              I have this array of strings: char str[90] = {" comp " Hello World
              " "};
              >
              And i whant to split in two arrays from the original: ( { <-----
              str1 ------- "<---str2 -------->" }
              >
              str1[10] = {comp}
              str2[80] = {Hello World}
              >
              sorry for my english
              Is pointer enough for your split?

              Comment

              • ric

                #8
                Re: How split 1 array in two

                James Fang escreveu:
                On Dec 2, 11:57 pm, ricardoramoscab ...@gmail.com wrote:
                >Hello to All.
                >>
                >I have this array of strings: char str[90] = {" comp " Hello World
                >" "};
                >>
                >And i whant to split in two arrays from the original: ( { <-----
                >str1 ------- "<---str2 -------->" }
                >>
                >str1[10] = {comp}
                >str2[80] = {Hello World}
                >>
                >sorry for my english
                >
                Is pointer enough for your split?

                I have pointer error :-(

                Comment

                Working...