Replace elements in string

This is a discussion on Replace elements in string within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm writing a function that takes a string and process it in a way to replace all occurences of ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    Replace elements in string

    Hello, I'm writing a function that takes a string and process it in a way to replace all occurences of "+-" with "-".
    This is what I've done:
    Code:
    void foo (string& str)
    {
    	string :: size_type ind;
    	ind = str.find ("+-");
    	while (ind != string :: npos)
    	{
    		str.replace (ind, strlen("+-"),"-");
    		ind = str.find ("+-", ind + 2);
    	}
    }
    And this works, but I wonder if this could be done in more elegant way.
    Please, give me some advices.
    Thanks
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Code:
    str.replace (ind, strlen("+-"),"-");
    Why do you keep calling strlen() on a string literal?
    Just because I don't care doesn't mean I don't understand.

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, 07:02 AM
  2. String replace
    By guitarist809 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2008, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

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