How to split a string into words in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

void SplitString(const char *input, const char *delims)
{
    char buffer[256] = "";
    strncpy(buffer, input, sizeof(buffer));
    buffer[sizeof(buffer)-1] = '\0';

    char *token = strtok(buffer, delims);

    while (token != NULL) {
        printf("%s\n", token);
        token = strtok(NULL, delims);
    }
}

int main(void)
{
    const char *s = "-c, c++.. c#:: -java ,php...python";
    const char *delims = " ,.: -";

    SplitString(s, delims);

    return 0;
}


/*
run:

c
c++
c#
java
php
python

*/

 



answered Feb 3, 2017 by avibootz
edited Aug 29 by avibootz
...