replacing character by a variable in a string.

I have a string like this:
80,15,2,?,0,71,9,16,?,5,6,7,?,32,5,67,96,?,8,95,100,675,875,0,?,?,?,0,5

I have replace all '?' with x which equals (rand()%10).
Can anyone help me with this code please???
I have read those threads but i am still not able to solve the problem.

I have to replace character '?' by a variable, lets suppose X=(rand() % 10), means it generates random values between 0 to 9.
I have to substitute those random values in the place of '?' in the whole string.

can anyone help me with this one?
why do repeat your problem?

Here's a start:
X=(rand() % 10)
then do this:
std::string strNum= std::to_string(X);

then you have your string which you need to insert in.
You could iterate over the original string and test each char to see if they are equal to '?'. if they are replace this with the string above.

Last edited on
I am still facing the problem, I dont know if i have done it correctly. Here is the code

String str = 80,15,2,?,0,71,9,16,?,5,6,7,?,32,5,67,96,?,8,95,100,675,875,0,?,?,?,0,5

X = (rand() % 10);
std::string strNum = std::to_string(X);

for (unsigned int i = 0; i < str.length(); i++)
{
if (str[i] == '?')
{
replace(str.begin(), str.end(), '?', X);
}
}
cout << str<< endl;
Last edited on
closed account (E0p9LyTq)
String str = 80,15,2,?,0,71,9,16,?,5,6,7,?,32,5,67,96,?,8,95,100,675,875,0,?,?,?,0,5 needs to be String str = "80,15,2,?,0,71,9,16,?,5,6,7,?,32,5,67,96,?,8,95,100,675,875,0,?,?,?,0,5"; I would be surprised if you didn't get compile-time errors.

Using replace(str.begin(), str.end(), '?', X); to do the character replacement will replace all the string's characters. Not what you are looking for, I would imagine.

Since you are checking each character to determine if each individual character in your string is a '?," replace the individual character. One possible method could be str[i] = strNum[0];.

You should seed the random number generator so you get a different random number each time the program runs. If you don't, the same number is used each time the program is run.

One possible code rewrite might be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <cstdlib> // for seeding random generator
#include <ctime>   // for getting the current system time

int main()
{
   std::string str = "80,15,2,?,0,71,9,16,?,5,6,7,?,32,5,67,96,?,8,95,100,675,875,0,?,?,?,0,5";

   // seed the random generator using the current system time
   srand(time(NULL));

   int X = (rand() % 10);
   std::string strNum = std::to_string(X);

   for (unsigned int i = 0; i < str.length(); i++)
   {
      if (str[i] == '?')
      {
         str[i] = strNum[0];
      }
   }
   std::cout << str << std::endl;
   return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>

int main()
{
    std::string str = "80,15,2,?,0,71,9,16,?,5,6,7,?,32,5,67,96,?,8,95,100,675,875,0,?,?,?,0,5";
    std::cout << str << '\n' ;

    // avoidable: use <random> instead
    std::srand( std::time(nullptr) ) ;
    const char random_decimal_digit = '0' + std::rand() % 10 ;
    
    std::cout << "replacing all '?' with '" << random_decimal_digit << "'\n" ;
    std::replace( str.begin(), str.end(), '?', random_decimal_digit ) ;
    std::cout << str << '\n' ;
}

http://coliru.stacked-crooked.com/a/5886783939cd04aa
closed account (E0p9LyTq)
I like your solution better, JLBorges. :) Nice reminder that C++11/C++14 offers much better alternatives.
Nice reminder that C++11/C++14 offers much better alternatives.

This was in the standard befor C++11 as far as I can tell, it has no C++11-mark in the reference page

std::srand( std::time(nullptr) ) ;
Why nullptr and not 0?
Last edited on
FurryGuy and JLBorges,

Thank you so much for helping me out.

FurryGuy's solution is more suitable with my requirement but I would like to you all for helping me out. :-)



closed account (E0p9LyTq)
Why nullptr and not 0?


Part of the reason why I said the code from JLBorges was a
Nice reminder that C++11/C++14 offers much better alternatives.

http://www.cplusplus.com/reference/cstddef/nullptr_t/

nullptr is a distinct fundamental C++ type, since C++11. One vastly preferred to my understanding over NULL or 0 (zero) in new code.

targaryen, you are most welcome. It was fun looking at your code and figuring out how to make it work, even if not one of the best solutions. :)
Last edited on
Part of the reason why I said the code from JLBorges was a Nice reminder that C++11/C++14 offers much better alternatives.
It's the only C++11 feature used there

nullptr is a distinct fundamental C++ type, since C++11. One vastly preferred to my understanding over NULL or 0 (zero) in new code.
I know but I thought time takes an integer and nullptr wouldn't be fit for it.
But I looked up the decleration of std::time and realized the parameter is a pointer to a time_t, so I was just wrong. :)
Topic archived. No new replies allowed.