Removing duplicates and replacing them with a string

This is a discussion on Removing duplicates and replacing them with a string within the C++ Programming forums, part of the General Programming Boards category; Hello, I have been trying to figure out how to remove multiple "XXXX" from a string and replace them with ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Removing duplicates and replacing them with a string

    Hello, I have been trying to figure out how to remove multiple "XXXX" from a string and replace them with the word "Siberia" once.

    Code:
    #include<iostream>
    #include<string>
    
    
    using namespace std;
    
    
    int main()
    
    
    {
         string str1 = "Luzer";
         string name = "<name>";
         string xxx = "X";
         string nothing = "";
         string lucky = "lucky";
         string str2 = "Siberia";
         string str3 = "un";
         size_t pos1, pos2, pos3;
         string longStr = "Congratulations Mrs. <name>, you and Mr. <name> are the lucky recipients of a trip for two to XXXXXX. Your trip to XXX is already scheduled";
    
    
         cout<<"Original String: \n\n"<<longStr<<endl<<endl;
    
    
         pos1=longStr.find(name);
         while(pos1!=string::npos)
         {
         
             longStr.replace(pos1, name.size(), str1);
             pos1=longStr.find(name, pos1+1);
         
         }
         
         pos2=longStr.find(lucky);
         longStr.insert(pos2, str3);
    
    
         pos3=longStr.find(xxx);
         while(pos3!=string::npos)
         {
             longStr.replace(pos3, xxx.length(),  str2);
             pos3=longStr.find(xxx, pos3);
             
         
         }
         
         longStr.append(" for December.");
    
    
         cout<<"\n"<<longStr<<"\n\n";
    
    
         system("pause");
         return 0;
    
    
    }
    this prints "Siberia" in every spot there is an "X". so "SiberiaSiberiaSib..." and not only once as intended. Any help would be much appreciated.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    After you find the position of the first X you need to find the position of the first character from that point that isn't an X. The difference between those two positions is the length of the substring that you want to replace. A caveat here is if there are no more non-X characters in the string, in which case the second position will be npos -- you'll have to deal with that as a special case.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    I see, so there is no one way of dealing with it. Well thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing duplicates and complexity
    By nik in forum C++ Programming
    Replies: 10
    Last Post: 01-27-2011, 01:01 PM
  2. duplicates in string
    By cvr in forum Linux Programming
    Replies: 0
    Last Post: 10-06-2010, 03:57 AM
  3. removing duplicates
    By satty in forum C Programming
    Replies: 4
    Last Post: 08-05-2010, 09:33 AM
  4. Removing Duplicates from an Array
    By SlayerBlade in forum C Programming
    Replies: 4
    Last Post: 10-02-2005, 05:46 PM
  5. removing duplicates from a linked list
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2003, 09:27 PM

Tags for this Thread


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