954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Spliting a string with a multiple character delimiter

Hello. I am wondering how to split a string lets say char *a="raining apples training away"
using a delimiter like "g a" and the result will be:
rainin
pples
trainin
way
I tried using strstr but I got stuck. Any hints will be greatly appreciated. Thank you in advance!

daianahoney
Newbie Poster
10 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 
Hello. I am wondering how to split a string lets say char *a="raining apples training away" using a delimiter like "g a" and the result will be: rainin pples trainin way I tried using strstr but I got stuck. Any hints will be greatly appreciated. Thank you in advance!


Assuming you want to use the whole string "g a" as a delimiter rather than any of the characters in that string, the output would be:

rainin
pples trainin
way

It's certainly possible to do this with strstr(), but you need to be careful of two things:Make sure to skip over the delimiter before testing again
Make sure to account for the last token as strstr() will return NULL when there's one more left
Here's an example:

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

int main(void)
{
    const char *src = "raining apples training away";
    const char *next = src;
    
    while ((next = strstr(src, "g a")) != NULL) {
        while (src != next)
            putchar(*src++);
        
        putchar('\n');
        
        /* Skip the delimiter */
        src += 3;
    }
    
    /* Handle the last token */
    while (*src != '\0')
        putchar(*src++);
        
    putchar('\n');
    
    return 0;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Assuming you want to use the whole string "g a" as a delimiter rather than any of the characters in that string, the output would be:

rainin pples trainin way

It's certainly possible to do this with strstr(), but you need to be careful of two things:

  • Make sure to skip over the delimiter before testing again
  • Make sure to account for the last token as strstr() will return NULL when there's one more left

Here's an example:

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

int main(void)
{
    const char *src = "raining apples training away";
    const char *next = src;
    
    while ((next = strstr(src, "g a")) != NULL) {
        while (src != next)
            putchar(*src++);
        
        putchar('\n');
        
        /* Skip the delimiter */
        src += 3;
    }
    
    /* Handle the last token */
    while (*src != '\0')
        putchar(*src++);
        
    putchar('\n');
    
    return 0;
}

You are right about the output, that is what I meant.
I am looking over the code now and running it in a debugger to better understand it and I'll come back with a reply afterwards. Thank you so much!

daianahoney
Newbie Poster
10 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Assuming you want to use the whole string "g a" as a delimiter rather than any of the characters in that string, the output would be:

rainin pples trainin way

It's certainly possible to do this with strstr(), but you need to be careful of two things:

  • Make sure to skip over the delimiter before testing again
  • Make sure to account for the last token as strstr() will return NULL when there's one more left

Here's an example:

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

int main(void)
{
    const char *src = "raining apples training away";
    const char *next = src;
    
    while ((next = strstr(src, "g a")) != NULL) {
        while (src != next)
            putchar(*src++);
        
        putchar('\n');
        
        /* Skip the delimiter */
        src += 3;
    }
    
    /* Handle the last token */
    while (*src != '\0')
        putchar(*src++);
        
    putchar('\n');
    
    return 0;
}


I fully understand, you print char by char while next and src are different because strstr returns a pointer to the first apparition of the substring in the string. After that you jump the number of characters equal to the delimiter and repeat.

It would've taken me a whole lot of time to get to this lovely solution so thank you very much again!

daianahoney
Newbie Poster
10 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: