Is there a way to split a string at different types of characters into separate strings?

char str[] ="(I) 44like22 .cookies. ,This, /is\ ?tricky?";

Something like this?

( I ) 44 like 22 . cookies . , This , / is \ ? tricky ?

You could try code like the below example, that uses these .h files:

Note: you can find needed (include) files as per below ...

CListOfString.h
http://developers-heaven.net/forum/index.php/topic,2582.msg2882.html#msg2882

readLine.h
http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

Clist.h
http://developers-heaven.net/forum/index.php/topic,2582.msg2877.html#msg2877

/* split2.h */ /* this version: 2015-08-07 */

/* http://developers-heaven.net/forum/index.php/topic,46.0.html */

#ifndef dwSPLIT_H
#define dwSPLIT_H

#ifndef DELIMITS
#define DELIMITS  " \t"
#endif

#define NUM_DLMTS  sizeof(DELIMITS) -1

#include "ClistOfString.h"
/* adds readLine.h which adds stdio.h, stdlib.h, string.h, myAssert, newCopy */
/* also adds "Clist.h" */

/*
CListOfString.h
http://developers-heaven.net/forum/index.php/topic,2582.msg2882.html#msg2882

readLine.h
http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

Clist.h
http://developers-heaven.net/forum/index.php/topic,2582.msg2877.html#msg2877
*/

#ifndef PUSH_CLIST
#define PUSH_CLIST  push_backClist
#endif



char* newSubStr( const char* start, const char* end )
{
    int len = end-start+1;
    char* newCpy = newMem(len);
    strncpy( newCpy, start, len );
    newCpy[len] = 0;
    return newCpy;
}

void split( Clist* lst, char* p1 )
{
    char *p2, *p3;
    List ml; /* ml is really a ... node/element/record in a List ... */
    for( ; ; ) /* loop forever ... until break */
    {
        p3 = p1;
        while( *p1 != 0 && strchr(DELIMITS, *p1) ) ++p1;

        if( p1 != p3 )
        {
            ml.str = newSubStr( p3, p1-1 );
            PUSH_CLIST( lst, &ml );
        }
        if( *p1 == 0 ) break; /* i.e. if empty or all delimits */

        p2 = p1+1;
        while( *p2 != 0 && !strchr(DELIMITS, *p2) ) ++p2;
        ml.str = newSubStr( p1, p2-1 ); /* new copy in new memory in ml.str */
        PUSH_CLIST( lst, &ml ); /* default is push_backClist */
        p1 = p2;
    }
}

#endif

Ok ... here is a little test program:

/* split2.h_test.c */  /* 2015-09-07 */

#define DELIMITS "() 42.,/\\?"

#include "split2.h"


int main()
{
    char str[] = "(I) 44like22 .cookies. ,This, /is\\ ?tricky?" ;

    /* example to compare of desrired putput ... */
    char str2[] = "( I ) 44 like 22 . cookies . , This , / is \\ ? tricky ?";

    pList cur;
    Clist cl;
    initClist( &cl );

    split( &cl, str );

    cur = cl.start;
    for( ; cur != NULL ; cur = cur->next )
         printf( "'%s' ", cur->str );

    printf("\ncl.size = %d\n", cl.size );

    printf("Compare to:\n%s\n", str2 );

    clearClist( &cl ); /* free all dynamic memory when done */

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.