I am trying to remove a string within a string. For example i have a string
char *temp = "Hi my name is xxx what is yours?";
i want to be able to remove the string "xxx" so that the resultant string is
"Hi my name is what is yours?"
Printable View
I am trying to remove a string within a string. For example i have a string
char *temp = "Hi my name is xxx what is yours?";
i want to be able to remove the string "xxx" so that the resultant string is
"Hi my name is what is yours?"
Your code above declares temp as a char pointer to a string literal. String literals are never modifiable. To make a modifiable string, you need a char array:
There's no standard function to remove a string from a string, so you'd need to search for what you want to remove (or already know the offset), then use something like memmove (or a loop) to move the rest of the string to the left so it overwrites what you want to remove. Have a go at this yourself. If you're stuck, I'll give you hints.Code:char temp[] = "Hi my name is xxx what is yours?";
Remember that a string in C is a null terminated char array, so you would want to move "what is yours?" to the left over the xxx including the null character ('\0') that occurs right at the end.
Look into strstr, strlen and memmove.
ok. thanks. i will see how i go