Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to split a string by substring delimiter in C

3 Answers

0 votes
#include <stdio.h>
#include <string.h>
 
void split_by_substring(char *str, const char *delimiter) {
    char *start = str;
    char *end;
 
    while ((end = strstr(start, delimiter)) != NULL) {
        *end = '\0';
        printf("%s\n", start);
        start = end + strlen(delimiter);
    }
    printf("%s\n", start);
}
 
int main() {
    char str[] = "C programming language and Dennis Ritchie and Bell Laboratories and popular";
    const char delimiter[] = "and"; // Substring delimiter
 
    split_by_substring(str, delimiter);
 
    return 0;
}
 
 
 
/*
run:
 
C programming language 
 Dennis Ritchie 
 Bell Laboratories 
 popular
  
*/

 



answered Jan 18, 2025 by avibootz
edited Jan 18, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

void trim(char *str) {
    char *start = str;
     
    while (*start == ' ') start++;
     
    char *end = str + strlen(str) - 1;
 
    while (end > start && *end == ' ') end--;
    *(end + 1) = '\0'; 
 
    // void *memmove(void *destination, const void *data_to_be_copied, size_t total_bytes)
    memmove(str, start, end - start);
}

void split_by_substring(char *str, const char *delimiter) {
    char *start = str;
    char *end;

    while ((end = strstr(start, delimiter)) != NULL) {
        *end = '\0';
        trim(start);
        printf("%s\n", start);
        start = end + strlen(delimiter);
    }
    trim(start);
    printf("%s\n", start);
}

int main() {
    char str[] = "C programming language and Dennis Ritchie and Bell Laboratories and popular";
    const char delimiter[] = "and"; // Substring delimiter

    split_by_substring(str, delimiter);


    return 0;
}



/*
run:

C programming language
Dennis Ritchiie
Bell Laboratoriees
populaar
 
*/
 

 



answered Jan 18, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
int split_by_substring(char *str, const char *delimiter, char *arr[]) {
    char *start = str;
    char *end;
    int count = 0;
 
    while ((end = strstr(start, delimiter)) != NULL) {
        *end = '\0';
        arr[count++] = start;
        start = end + strlen(delimiter);
    }
    arr[count++] = start;
    
    return count;
}
 
int main() {
    char str[] = "C programming language and Dennis Ritchie and Bell Laboratories and popular";
    const char delimiter[] = "and"; // Substring delimiter
    char *arr[16];
     
    int count = split_by_substring(str, delimiter, arr);
     
    for (int i = 0; i < count; i++) {
        printf("%s\n", arr[i]);
    }
 
    return 0;
}
 
 
 
/*
run:
 
C programming language 
 Dennis Ritchie 
 Bell Laboratories 
 popular
  
*/

 



answered Jan 18, 2025 by avibootz

Related questions

...