How split 1 array in two

Discussion in 'C Programming' started by ricardoramoscabral@gmail.com, Dec 2, 2007.

  1. Guest

    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
     
    , Dec 2, 2007
    #1
    1. Advertising

  2. 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).
     
    Martin Ambuhl, Dec 2, 2007
    #2
    1. Advertising

  3. Guest

    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 );
    }
     
    , Dec 2, 2007
    #3
  4. ric Guest

    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

    Can you give some tips / help.

    Thanks.
     
    ric, Dec 2, 2007
    #4
  5. Thad Smith Guest

    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
     
    Thad Smith, Dec 2, 2007
    #5
  6. CBFalconer Guest

    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
     
    CBFalconer, Dec 2, 2007
    #6
  7. James Fang Guest

    On Dec 2, 11:57 pm, 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?
     
    James Fang, Dec 3, 2007
    #7
  8. ric Guest

    James Fang escreveu:
    > On Dec 2, 11:57 pm, 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 :-(
     
    ric, Dec 8, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    492
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    742
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    241
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    278
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    713
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page