How to split a string at the first occurrence of a separator in C

1 Answer

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

int main() {
    char str[] = "java go c c++ python c#";

    int pos = strstr(str, "c") - str;

    char part1[100] = "", part2[100] = "";

    if (pos != -1) {
        strncpy(part1, str, pos);
        part1[pos] = '\0';
        strcpy(part2, str + pos);
    }

    printf("%s\n", part1);
    printf("%s\n", part2);

    return 0;
}



/*
run:

java go
c c++ python c#

*/

 



answered May 23, 2024 by avibootz

Related questions

2 answers 211 views
1 answer 112 views
1 answer 122 views
1 answer 141 views
2 answers 228 views
...