1 Replies - 185 Views - Last Post: 08 October 2012 - 04:16 PM Rate Topic: -----

#1 emk0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-September 12

Replacing . (dots) in a string with numbers from another string

Posted 08 October 2012 - 03:42 PM

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 (replaced?) string shout be the following:
something 111 something 000

i have written the following code:

#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'.

Is This A Good Question/Topic? 0
  • +

Replies To: Replacing . (dots) in a string with numbers from another string

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Replacing . (dots) in a string with numbers from another string

Posted 08 October 2012 - 04:16 PM

This works for me.
  string s= "Testing. One. two. three.";
	string r= ".2";
	
	char replce, with;
	replce = r[0];
	with = r[1];

  replace( s.begin(), s.end(), replce, with);
  cout << s << endl;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1