Remove a character from a string?

This is a discussion on Remove a character from a string? within the C++ Programming forums, part of the General Programming Boards category; Hi, for example I have a string that contains the word "hello" Now I want to remove all the "l"s ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    43

    Remove a character from a string?

    Hi, for example I have a string that contains the word "hello"

    Now I want to remove all the "l"s from the word hello, so make it show heo.


    How do I go about it in an easy way? I don't want stuff complicated. Make it as simple as possible. Thanks.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: Remove a character from a string?

    Originally posted by trenzterra
    Hi, for example I have a string that contains the word "hello"

    Now I want to remove all the "l"s from the word hello, so make it show heo.


    How do I go about it in an easy way? I don't want stuff complicated. Make it as simple as possible. Thanks.
    I'm not going to make it for you because you don't learn by copying code from someone else.
    How about looping through the string and copy all char's that are not equal to 'l'?
    Code:
    J = 0
    CH = 'l'
    FOR I = 0 TO LENGTH(MSG)
       IF MSG[I] <> CH THEN
          BUF[J] = MSG[I]
          J = J + 1
       END IF
    END FOR
    BUF[J] = '\0'

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I didn't think or it long enough, but maybe you can have a char * then using that pointer check out for every character and remove what you want, I think it's simple.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    Well I haven't learnt about pointers... So how will char* work?

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    It would be better if you explained?

  6. #6
    endo_at_work
    Guest
    Originally posted by trenzterra
    Well I haven't learnt about pointers... So how will char* work?
    Imagine the characters of the string sitting next to each other in memory. I always thinks of pointers as arrows pointing at areas of memory, so you will start with 2 pointers pointing to the start of the string.

    Now you need to move both pointers along by incrementing them (this works in a similar way to incrementing ints). Use one pointer to skip over the l's and the other to keep track of the back of the string you are building. When you finish dont forget to add the NULL to the end!

    I'm not sure if this will work, no compiler at work - but it is a similar function to remove spaces from a string

    Code:
    void stripSpace( char* string, int length )
    {
    	char* ptr1 = string;
    	char* ptr2 = string;
    
    	while( *ptr2 != NULL )
    	{
    		if( *ptr2 == ' ' )
    		{
    			ptr2++;		//skip l's
    		}
    		*ptr = *ptr2;
    
    		//move both pointers along 1
    		ptr1++;
    		ptr2++;
    	}
    }

  7. #7
    endo_at_work
    Guest

    oops

    I forgot to add the '\0' to the end, sorry

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The C++ version:
    Code:
    #include <string>
    #include <iostream>
    
    int main(void)
    {
       string msg = "Hello world";
       for(int i = 0; (i = msg.find('l', i)) != -1; msg.erase(i, 1)) ;
       cout << msg << endl;
       return 0;
    }

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    The C++ version:
    It's even easier than that:
    Code:
    #include <string>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string s = "Hello, world!";
    
        cout << s << '\n';
    
        s.erase(remove(s.begin(), s.end(), 'l'), s.end());
    
        cout << s << '\n';
    }
    - lmov

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21