Thread: String Remove problem

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

    String Remove problem

    I am trying to make a function that takes a string and removes all occurences of a specified character then returns the new string. The code I wrote generates an error, and I am having no luck debugging the problem. Can someone point me in the write direction? I am using visual studio 2003.

    Code:
    char* remove(char *str, char c)
    {
    	char *str2="";
    
    	while(*str != '\0')
    	{
    		if(*str!=c)
    		{
    			*str2=*str;
    		}
    
    		str++;
    		str2++;
    	}
    	return str2;
    }
    int _tmain()
    {
    
    	char* str = "This is a test";
    	char c= ' ';
    	remove(str, c);
    //	cout<<endl<<remove(str, c);
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    In C++, why not use std::string?

    Don't try to return a pointer to a local.

    Only write to modifiable memory.

    Search the board.
    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.*

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    char *str2="";
    That points to an immutable string literal. You want it to point to some free space. You might do something like;

    Code:
    char *str2 = new char[strlen(str) + 1];
    And then delete[] the return of remove(..) to avoid a memory leak. Alternately, you could pass the function an existing character buffer pointing to free space.
    Last edited by Tonto; 02-23-2006 at 10:09 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    There are some constraints that I can not remove on the code. The pointers can not be removed.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    When I return my new string I am getting a trash value that is not useful.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    No I am supposed to return another string that contains the proper output. The original string should remain unchanged.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    [Sorry for the deletion. I reread that.]

    And I suppose your function cannot change to receive a destination string?
    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.*

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You modify the value of your str2. You never null-terminate it, and return a pointer to where the null-terminator should be. Your algorithm has something a little wrong. To fix the first problem, do something like:

    Code:
    	char *space = new char[strlen(str) + 1];
    	char *temp = space;
    
    	... 
    	... // Operate on 'temp'
    	...
    
    	return space;

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    Okay I fixed the remove method to look like this
    Code:
    char* remove(char *str, char c)
    {
    	char *space = new char[strlen(str) + 1];
    	char *str2= space;
    
    	while(*str != '\0')
    	{
    		if(*str!=c)
    		{
    			*str2=*str;
    			str2++;
    		}
    
    		str++;
    		
    	}
    	*str2='\0';
    	return space;
    }
    but why do I return space and not temp?

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Because you change the value of the pointer so by the end you would be returning a pointer to the end of the string. If you just keep a pointer to the start of the space, and manipulate it with another (or bypass this whole thing entirely with array notation), then you can just return the pointer to the new space string.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    Thanks Tonto you cleared all that up for me. How would you have done this if you were given the problem, and why would you do it that way? I am still learning, and I want to know the right way to do things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 2
    Last Post: 06-21-2006, 04:23 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM