How to get a substring between two indexes of a string in C

1 Answer

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

int main() {
    char s[] = "c++ python c java";
    int start = 2, end = 6;
    char substr[5]; // end - start = 4 + 1 for null terminator

    strncpy(substr, s + start, end - start);
    substr[end - start] = '\0'; // null terminate the string

    printf("%s\n", substr);
    
    return 0;
}



/*
run:

+ py

*/

 



answered Sep 30, 2024 by avibootz

Related questions

1 answer 214 views
1 answer 930 views
1 answer 107 views
1 answer 122 views
1 answer 117 views
...