String Replacing

replace parts of a string with another string

Page 1 of 1

4 Replies - 1090 Views - Last Post: 11 June 2010 - 09:16 PM Rate Topic: -----

#1 woppix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 112
  • Joined: 13-September 09

String Replacing

Posted 11 June 2010 - 12:34 AM

i'm working on one of the projects in a data structures book and came to a slight issue when it asked me replace part of a string using only find(), insert(), and erase(). What i'm trying to accomplish is to have <pat> anywhere in the string (including multiple places in the string) and be able to replace it through strReplace in the strClass class. In the class strA is the full string, strB is what is going to be replaced, and strC is what i need to replace strB with. The issue i'm having is doing the actual calculations to delete strB and insert strC in its place. As of now i just have the position parameters set, but of course it by far doesn't accomplish what i need it to. Any advice would help me a lot.

void strClass::strReplace(string& strA, const string& strB, const string& strC)
{
	//cout << "This is being called";
	size_t found;
	//size_t pos;

	found=strA.find(strB, 0);
	if (found!=string::npos)
	{
		strA.insert(0, strC);
		strA.erase(3, strB.length());
		//strA.insert(
		cout << strA << "\n\n";
		
	}
}





main
#include "strClass.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	strClass test;

	string str;
	const string str2 = "<pat>";
	const string str3 = "iss";

	cout << "Enter a string: ";
	getline (cin, str);
	//cout << "Enter replacement string: ";
	//getline (cin, str2);

	test.strReplace(str,str2,str3);
	//strClass::strInsert


	//temporary
	system("PAUSE");

}


This post has been edited by woppix: 11 June 2010 - 12:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: String Replacing

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: String Replacing

Posted 11 June 2010 - 01:07 AM

All you need to do is to find the position(s) of strB in strA. Then, starting with this position(s), delete strB.length() characters from strA. Then, simply insert strC, in strA, starting with that position(s). This works:
#include <iostream>
#include <string>

// input is your strA equivalent
// to_replace is strB
// need_to_replace is strC
void replace_substrings(std::string &input, const std::string &to_replace, const std::string &need_to_replace)
{
        // Couldn't find to_replace in the original string? Then there's no
        // need to continue anymore.
	if (input.find(to_replace) == std::string::npos)
		return;

	size_t pos;
        // Simply loop until no more to_replace substrings are found. pos gets
        // the position of to_replace, in the original string
	while ((pos = input.find(to_replace)) != input.npos)
	{
                // Erase to_replace.length() characters starting with the position
                // of to_replace.
		input.erase(pos, to_replace.length());
                // Then, insert need_to_replace
		input.insert(pos, need_to_replace);
	}
}

int main()
{
        // Replace "ll" substring with "hey" substring.
	std::string al = "hello therll";
	replace_substrings(al, "ll", "hey");
	std::cout << al << "\n";

	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#3 woppix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 112
  • Joined: 13-September 09

Re: String Replacing

Posted 11 June 2010 - 01:28 AM

thank you, that really helped a lot with all the comments you added to explain what everything does. I appreciate the time spent to help me figure out a way to accomplish this.
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,340
  • Joined: 20-August 07

Re: String Replacing

Posted 11 June 2010 - 12:26 PM

If you're planning on this type of operation in production code (And if you're using a newer compiler which supports TR1), then take advantage of the soon-to-be standard Regular Expressions library
#include <iostream>
#include <string>
#include <regex>

int main()
{
    const std::string str("The <pat> <pat> on the <pat>");
    const tr1::regex pattern("<pat>");
    const std::string substitute("iss");

    std::string replaced_str = std::tr1::regex_replace(str, pattern, substitute);

    std::cout << replaced_str << std::endl;
} 


For more info: http://www.johndcook.../cpp_regex.html
Was This Post Helpful? 3
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: String Replacing

Posted 11 June 2010 - 09:16 PM

View PostBench, on 12 June 2010 - 04:26 AM, said:



Excellent link!
Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1