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!

Recommended Answers

All 3 Replies

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:

  1. Make sure to skip over the delimiter before testing again
  2. 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;
}

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:

  1. Make sure to skip over the delimiter before testing again
  2. 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!

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:

  1. Make sure to skip over the delimiter before testing again
  2. 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!

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.