Search and replace string in C++


/*
 * session7.cpp
 *
 *  Created on: Aug 11, 2013
 *      Author: kobee
 */

#include <iostream>
#include <string>

using namespace std;

bool search_and_replace(string &str, const string &oldsubstr,const string &newsubstr);

int main()
{

	string str = "This is a test. So is this.";

	cout << "Original string: " << str << "\n\n";

	cout << "Replacing 'is' with 'was':\n";

	// The following replaces is with was. Notice that
	// it passes string literals for the substrings.
	// These are automatically converted into string objects.
	while(search_and_replace(str, "is", "was"))
		cout << str << endl;

	cout << endl;

	// Of course, you can explicitly pass string objects, too.
	string oldstr("So");
	string newstr("So too");
	cout << "Replace 'So' with 'So too'" << endl;
	search_and_replace(str, oldstr, newstr);
	cout << str << endl;

	return 0;
}

// In the string referred to by str, replace oldsubstr with newsubstr.
// Thus, this function modifies the string referred to by str.
// It returns true if a replacement occurs and false otherwise.
bool search_and_replace(string &str, const string &oldsubstr,
		const string &newsubstr) {
	string::size_type startidx;

	startidx = str.find(oldsubstr);
	if(startidx != string::npos) {
		str.replace(startidx, oldsubstr.size(), newsubstr);
		return true;
	}

	return false;
}

Advanced

/*
 * session8.cpp
 *
 *  Created on: Aug 11, 2013
 *      Author: kobee
 */

// Implement case-insensitive search and search-and-replace
// for string objects.
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

using namespace std;

bool comp_ign_case(char x, char y);
string::iterator search_ign_case(string &str, const string &substr);
bool search_and_replace_ign_case(string &str, const string &oldsubstr,
		const string &newsubstr);

int main()
{
	string strA("This is a test of case-insensitive searching.");
	string strB("test");
	string strC("TEST");
	string strD("testing");

	cout << "First, demonstrate search_ign_case().\n";
	cout << "String to be searched:\n" << strA << "\n\n";

	cout << "Searching for " << strB << ". ";
	if(search_ign_case(strA, strB) != strA.end())
		cout << "Found!\n";

	cout << "Searching for " << strC << ". ";
	if(search_ign_case(strA, strC) != strA.end())
		cout << "Found!\n";

	cout << "Searching for " << strD << ". ";
	if(search_ign_case(strA, strD) != strA.end())
		cout << "Found!\n";
	else
		cout << "Not Found.\n";

	// Use the iterator returned by search_ign_case() to display
	// the remainder of the string.
	cout << "\nRemainder of string after finding 'of':\n";
	string::iterator itr = search_ign_case(strA, "of");
	while(itr != strA.end())
		cout << *itr++;
	cout << "\n\n";

	// Now, demonstrate search and replace.
	strA = "Alpha Beta Gamma alpha beta gamma";
	cout << "Now demonstrate search_and_replace_ign_case().\n";
	cout << "String that will receive replacements:\n" << strA << "\n\n";
	cout << "Replacing all occurrences of alpha with zeta:\n";
	while(search_and_replace_ign_case(strA, "alpha", "zeta"))
		cout << strA << endl;

	return 0;
}

// Ignore case when searching for a substring.
// The string to search is passed in str. The substring to search
// for is passed in substr. It returns an iterator to the start of
// the match or str.end() if no match is found.
//
// Notice that it uses the search() algorithm and specifies the
// binary predicate comp_ign_case().
string::iterator search_ign_case(string &str, const string &substr) {
	return search(str.begin(), str.end(),
			substr.begin(), substr.end(),
			comp_ign_case);
}

// Ignore case when comparing two characters for equality.

// Return true if the characters are equal, independently
// of case differences.
bool comp_ign_case(char x, char y) {
	return tolower(x) == tolower(y);
}

// This function replaces the first occurrence of oldsubstr with
// newsubstr in the string passed in str. It returns true if a
// replacement occurs and false otherwise.
//
// Note that this function modifies the string referred to by str.
// Also note that it uses search_ign_case() to find the substring
// to replace.
bool search_and_replace_ign_case(string &str, const string &oldsubstr,
		const string &newsubstr) {
	string::iterator startitr;

	startitr = search_ign_case(str, oldsubstr);

	if(startitr != str.end()) {
		str.replace(startitr, startitr+oldsubstr.size(), newsubstr);
		return true;
	}

	return false;
}

reference : Herb Schildt’s C++ Programming Cookbook

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: