String Slicing in C

mail@pastecode.io avatar
unknown
c_cpp
a month ago
392 B
2
Indexable
Never
#include <stdio.h>

void slice(char str[], int m, int n) {
    int i, j = 0;
    
    for (i = m; i <= n && str[i] != '\0'; i++) {
        str[j++] = str[i];
    }
    str[j] = '\0'; // Null terminate the sliced string
}

int main() {
    char str[] = "Hello, World!";
    int start = 7, end = 11;
    
    slice(str, start, end);
    printf("Sliced string: %s\n", str);
    
    return 0;
}
Leave a Comment