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?"
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.
Last edited by Dave_Sinkula; 02-15-2006 at 09:20 PM. Reason: D'oh!
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
ok. thanks. i will see how i go