How to cut out a section of string in C

1 Answer

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

int cut_out_a_section_of_string(char str[], const int index_from, const int index_to) {
    if (index_from < 0 || index_to < 0)
        return -1;

    int sectionlen = index_to - index_from;
    int stringlen = strlen(str);

    if (sectionlen < 0)
        return -1;

    memmove(str + index_from, str + index_from + sectionlen, stringlen - index_to);
    str[stringlen - sectionlen] = '\0';

    return 0;
}

int main(void)
{
    //               01234567 
    char string[] = "C Programming";

    cut_out_a_section_of_string(string, 4, 7);

    printf("%s", string);

    return 0;
}




/*
run:

C Pramming

*/

 



answered Nov 17, 2023 by avibootz

Related questions

1 answer 110 views
110 views asked Nov 17, 2023 by avibootz
1 answer 126 views
1 answer 219 views
219 views asked Jul 28, 2017 by avibootz
1 answer 128 views
1 answer 165 views
165 views asked Jan 25, 2018 by avibootz
1 answer 127 views
...