Replacing and finding chars in a string

This is a discussion on Replacing and finding chars in a string within the C++ Programming forums, part of the General Programming Boards category; I'm looking to find out how to find characters in one string and replace characters in another string with the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Replacing and finding chars in a string

    I'm looking to find out how to find characters in one string and replace characters in another string with the same character in the same position as the ones that were found in the first string...

    I hope that makes sense.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Not really, sorry!

    But, assuming you are using the C++ string class, you can find a character or substring of characters using find, find_first_of, find_last_of, etc. You can also replace characters or strings using replace. If you combine the use of these functions you could probably do what you want, especially considering the find returns an index to the location of the found character that you could use to replace a character at the same index in another string.

  3. #3
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61
    Because I can't.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    I'm having issues with the replace method. When I include it in the below code, it compiles fine, but when I reach that section of code when I run the program, it mysteriously aborts...

    Anyone know why?

    Code:
    for (int i = 0; i < dashWord.length(); i++)
    {
    		
    x = word.find_first_of(guess,i);
    word.replace(x,1,guess);
    i = x;
    cout << dashWord;
    		
    }

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    string name;
    
    name = "blob";
    
    bob[0] = 'f';
    
    cout << name;  //flob
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You should check the return value of find_first_of in case it wasn't found. If it wasn't, I believe string::npos is returned (which is basically -1), and so you can't use it with replace.

    Also, I think find is sufficient instead of find_first_of, but I don't know what you are doing. Finally, maybe it should be dashWord.find(guess, i); instead of word.find(...). That's just a guess based on your description above of what you are trying to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble isolating special utf-8 character in string
    By myrddinemrys in forum C Programming
    Replies: 3
    Last Post: 12-30-2007, 03:21 AM
  2. Replacing a substring with another
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 09-14-2006, 11:37 AM
  3. Question About Finding a Word and Replacing It
    By Zildjian in forum C Programming
    Replies: 3
    Last Post: 09-23-2003, 09:50 AM

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