I wouldn't mind writing my own function to do this but I was wondering if there existed one in the string.h or if there was a standard way to do this.
char *string = "This is a string";
strcut(string, 4, 7);
printf("%s", string); // 'This a string'
Thanks!
Use memmove to move the tail, then put '\0' at the new end position. Be careful not to use memcpy - its behaviour is undefined in this situation since source and destination usually overlap.
You can just tell printf to cut the interesting parts out for you:
char *string = "This is a string";
printf("%.*s%s", 4, string, &string[7]); // 'This a string'
:-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With