1

My string is std::string str ("DDWD");

I want to change the corresponding to the following

D = [0-9] W = [a-z]

soo...("[0-9][0-9][A-Z][0-9]")

The replace method does not seem to accomade this, so i tried something like

string tmp = "DDDWD";   
int len = 0;   
len = tmp.length();   
for( int i = 0; i < len; i++ )
{
    if ( tmp[i] == 'D')
    {  
        tmp.replace(i,1,"[0-9]");  
        i+=2;  
    }
} 

However trying to change both letters D and W there was a problem and it wasent changing them all correctly?

Does anyone know a way how to change each letter at the same time to the corresponding string above?

Thankful for any help

Alok Save
  • 202,538
  • 53
  • 430
  • 533
CodersSC
  • 710
  • 2
  • 13
  • 29

5 Answers5

3

Looks to me like you're just ending your loop early.

len = tmp.length();

for( int i = 0; i < len; i++ )

{

...so you save the length to iterate, and even if the string gets longer, you'll stop at the old length.

sje397
  • 41,293
  • 8
  • 87
  • 103
2

I think this would be cleaner if you used a separate string:

string tmp = "DDDWD";
string tmp2;

for (int i=0; i<tmp.size(); ++i)
{
    if (tmp[i] == 'D')
        tmp2 += "[0-9]";
    else if (tmp[i] == 'W')
        tmp2 += "[a-z]";
    else
        tmp2 += tmp[i];
}

tmp.swap(tmp2);

// or in C++11
// tmp = std::move(tmp2);
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

The replace method works with positions, not substrings. So replacing would be a two-step process: find first, then replace with the results of the find, until find no longer finds anything.

But the way you do it also works, if you run the loop backwards (start at the end and work your way down).

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
0

If you can afford it, use Boost's String Algo library. It has lots of useful algorithms on strings. Your problem would then read:

  // #include <boost/algorithm/string/replace.hpp>
  std::string tmp = "DDDWD"; 
  boost::replace_all( tmp, "D", "[0-9]" );
  boost::replace_all( tmp, "W", "[a-z]" );
Andrzej
  • 5,027
  • 27
  • 36
-1
int main()
{
  std::string tmp = "DDDWD";
  char *tmp2=new char(5*(strlen(tmp.c_str()) +1)); 
  cout << "tmp is " << tmp;


  for (int i=0; i<tmp.size(); ++i)
  {
    if (tmp[i] == 'D' && i==0)
    tmp2 = strcpy(tmp2,"[0-9]");
    else if(tmp[i] == 'D' && i!=0) 
    tmp2 = strcat(tmp2,"[0-9]");   
    else if (tmp[i] == 'W' && i==0)
    tmp2 = strcpy(tmp2,"[a-z]");
    else if (tmp[i] == 'W' && i!=0)
    tmp2 = strcat(tmp2,"[a-z]");
  }

  cout << "tmp2 is :" << tmp2;

  getchar();   
}
Invictus
  • 4,028
  • 10
  • 50
  • 80
  • 1
    If you null-terminate tmp2 from the start, you can just use strcat everytime. Then you don't need the extra logic of checking if `i==0` or `i!=0` – Benjamin Lindley Jan 07 '12 at 17:24
  • @Andrzej I tried implementing the boost thing you mentioned , but i get an error saying it is not able to include replace.hpp file as you mentioned . Is it not available in standard C++ – Invictus Jan 08 '12 at 10:31