Replacing . (dots) in a string with numbers

Hi i have the task to type in the following numbers
111000 in a string, and then type this format in another string
something ... something ...
the final string shout be the following:
something 111 something 000

i have written the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
  {
  string ss; //do not use it here
  string s;
  cout << "write some 1 and 0 "<< endl;
  getline (cin, ss);
  cout << "write some . (dots) and some text"<< endl;
  getline (cin, s);
  replace( s.begin(), s.end(), '.', '1' );
  cout << s << endl;
    system("pause");
  return 0;
}


with this code i am testing replace(), but I still cant find any solution to implement string ss so that the dots can be replaced by the content of ss string and not by '1'.
Last edited on
I think you may have to use a loop to examine each individual character of s to see if it is '.' and replace that character with successive characters from ss.
Exit the loop when the last character of either ss or s has been reached.
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
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>

int main()
{
   std::string replacement;

   std::cout << "Write some 1 and 0 ";
   std::cin >> replacement;

   std::string source;

   std::cout << "\nWrite some . (dots) and some text ";
   std::cin>> source;

   const char *p = replacement.c_str();

   std::transform( source.begin(), source.end(), source.begin(),
                   [p]( char c ) mutable
                   {
                      return ( ( c == '.' && *p ) ? *p++ : c );
                   } );


   std::cout << "\nThe result " << source << std::endl;

   std::system( "Pause" );

   return ( 0 );
}   

Last edited on
Topic archived. No new replies allowed.