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.

39,994 questions

51,939 answers

573 users

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 99 views
99 views asked Nov 17, 2023 by avibootz
1 answer 109 views
1 answer 208 views
208 views asked Jul 28, 2017 by avibootz
1 answer 115 views
1 answer 153 views
153 views asked Jan 25, 2018 by avibootz
1 answer 112 views
...