Thread: removing a string in a string

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    31

    removing a string in a string

    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?"

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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:
    Code:
    char temp[] = "Hi my name is xxx what is yours?";
    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.

    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.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    31
    ok. thanks. i will see how i go

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Removing junk from end of string
    By dirkduck in forum C++ Programming
    Replies: 7
    Last Post: 05-20-2002, 01:15 PM