Do I need to change the line in place, or is it just the result that needs to be undone?
If the first, then you have a problem. If the declaration is valid
char *ch = "krishna is the best";
then you are trying to change the string literal, and the behavior when trying to change the string literal is undefined. If you work on a platform where string literals are stored in read-only memory, you will get a runtime error. You need to either change the announcement to
char ch[] = "krishna is the best";
or select a dynamic buffer and copy the contents of the string into it
char *ch = "krishna is the best"; char *buf = malloc(strlen(ch) + 1); if (buf) { strcpy(buf, ch);
to complete a U-turn.
If this is just the result that needs to be changed, then storage is not a big deal, you just need some pointers to keep track of the beginning and end of each substring. For instance:
#include <stdio.h>
Probably the best way to do this is simply from the head.
source share