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.

40,036 questions

52,001 answers

573 users

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

...