I\'m trying to figure out how to cut part of a string in C. For example you have this character string \"The dog died because a car hit him while it was crossing the road\" how
You can use something similar to Python's [n:m]
cut operator via a simple code but involves dynamic allocation and also preserve the original string that is an input.
char* cutoff(const char* str, int from , int to)
{
if (from >= to)
return NULL;
char* cut = calloc(sizeof(char), (to - from) + 1);
char* begin = cut;
if (!cut)
return NULL;
const char* fromit = str+from;
const char* toit = str+to;
(void)toit;
memcpy(cut, fromit, to);
return begin;
}