Replace function

This is a discussion on Replace function within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to write a little program that will replace a charter in a string, for example if ...

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Unhappy Replace function

    Hi I am trying to write a little program that will replace a charter in a string, for example if there is a “5”, replace it with a “?” I have been playing around with the “replace” function but cant get it to work.
    Code:
    char rep[] = {5};
        int i, pos;
        string sen;
    
        cout << "Please enter a sentence ";
        getline (cin, sen);
    
        for (i = 0; i < sizeof(rep); i++)
        {
            while ((pos = sen.find(repl[i])) != string::npos)
            {
                sen[pos] = replace  // This is where I am lost? 
       	}
        }
        cout << sen;
    I have read every page that i cam find on google, and i am still lost... Please help!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The replace function is a member function of the string class, so you should probably be calling it like you call find(). Then it's just a matter of putting in the right parameters.

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Daved View Post
    The replace function is a member function of the string class, so you should probably be calling it like you call find(). Then it's just a matter of putting in the right parameters.
    Cheers mate, I got it working,

    Code:
        int i, pos;
        string sen;
    
        cout << "Please enter a sentence ";
        getline (cin, sen);
    
        char replace = 48;
        string s2 = "?";
        for (i = 0; i < sizeof(replace); i++)
        {
            while ((pos = sen.find(replace)) != string::npos)
            {
                sen.replace( pos, 1, s2 );
            }
        } 
        cout << sen;
    But at the moment it can only get it to change one character, "0" if i wanted it to change 0,1,2,3,4,5,6,7,8,9

    would i change

    Code:
        char replace = 48;
    to (i know this doesn't work)
    Code:
    char replace = 48,49,50,51,52,53,54,55,56,57;
    how do it do it? or would i have to copy that over and over again?

  4. #4
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    All good, worked it out

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> All good, worked it out
    Great! Feel free to post your solution so if somebody searches with the same problem they can benefit.

    >> char replace = 48;
    There's no reason to use 48 here. It is a magic number and not everybody will know what it stands for. When you want to specify a single character, just put it in single quotes:
    Code:
    char replace = '0';
    That will work no matter what the character set being used is.

  6. #6
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Daved View Post
    >> All good, worked it out
    Great! Feel free to post your solution so if somebody searches with the same problem they can benefit.

    >> char replace = 48;
    There's no reason to use 48 here. It is a magic number and not everybody will know what it stands for. When you want to specify a single character, just put it in single quotes:
    Code:
    char replace = '0';
    That will work no matter what the character set being used is.
    No worries mate

    Code:
        char replaceNUM[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 
        int i, pos;
        string sen;
    
        cout << "Please enter a sentence " << endl;;
        getline (cin, sen);
        
        string newChar = "?";
        for (i = 0; i < sizeof(replaceNUM); i++)
        {
            while ((pos = sen.find(replaceNUM[i])) != string::npos)
            {
                sen.replace( pos, 1, newChar );
            }
        }
    If anyone wants more info on the "replace" have a read of this http://www.cppreference.com/cppstring/replace.html i found it to be quite useful
    Last edited by Loic; 04-02-2007 at 05:53 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    endl;;
    Double semicolons might look nice, but they're unnessesary.

    BTW, pos should probably be declared as
    Code:
    string::size_type pos;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Note also, that the string find method has useful relatives, for example find_first_of:

    Code:
    std::string replaceChars("123456789");
        std::string newChar = "?";
        
        while ((pos = sen.find_first_of(replaceChars)) != std::string::npos)
        {
            sen.replace( pos, 1, newChar );
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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