Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replaces strings in a txt file 1

Status
Not open for further replies.

Edge118

Programmer
Jun 15, 2004
104
US
Hi,
I have a .txt file that I am reading in line by line, and on each line I want to find and replace certain words. Is there a function that is available that will replace the word with another word if it is in the line multiple times? If not, is there an easy way to do this? I was looking at the "at" function, but that only finds one instance of the word, so if there would be more than one occurance of the word on the line, I would be SOL.

Thanks for your help.

"Pin me. Perl me."

Regards,

Chris
 
Hi,

See this "replace" function, using STL strings :
Code:
##include <assert.h>
#include <iostream>

#include <string>
using namespace std;

//Replace in string 'str' all occurences of string 'searched' by 'substitute'.
void replace (string& str,
	      const string& searched, const string& substitute)
{
  int pos = str.find (searched);
  while (pos != string::npos)
    {
      str.replace (pos, searched.length (), substitute);
      pos = str.find (searched);
    }

  assert (str.find (searched) == string::npos);//all_replaced
}

//A little test.
int main ()
{
  string test = "haha hohohaha pfiou";

  cout << test << endl;

  replace (test, "haha", "aa");

  cout << test << endl;
  assert (test == "aa hohoaa pfiou");

  return 0;
}

--
Globos
 
Thanks alot. The above code is very useful. Another quick question I have: Is it possible to match the one string to another without worrying about uppercase/lowercase letters? Originally I was going to change the entire string to lowercase before searching through it, but ultimately I would need to put the capitalization back into the original string along with the replaced words, before I copied it over to another file.

Do you know of an easy way to do this?

Thanks again.

"Pin me. Perl me."

Regards,

Chris
 
Hi,

The solution I give below is a bit more complicated. It is based on generic STL 'search' algorithm, that can parameterized with the comparator to be used.

See the modified code :
Code:
#include <cstring>//needed for stricmp
#include <string>
#include <algorithm>//needed for search
using namespace std;

// Binary predicate : a case insensitive equality comparator
struct CharCaseLessEqualityComparator
{
  bool operator () (char x, char y)
  {
    return stricmp (&x, &y) == 0;
  }
};

// Returns an iterator that targets in 'str' the first occurence of
// string 'sought'.
// Character equality comparison is based on the actual generic
// parameter 'eq'.
template<class Comparator>
string::iterator string_search (string& str, const string& sought,
				const Comparator& eq)
{
  return search (str.begin (), str.end (),
		 sought.begin (), sought.end (), eq);
}

// Replace in string 'str' all occurences of string 'sought'
// by 'substitute'.
void string_replace (string& str,
		     const string& sought, const string& substitute)
{
  string::iterator pos =
    string_search (str, sought, CharCaseLessEqualityComparator ());
  while (pos != str.end ())
    {
      str.replace (pos, pos + sought.length (), substitute);
      pos = string_search (str, sought, CharCaseLessEqualityComparator ());
    }
}


//Little test
#include <assert.h>
#include <iostream>

int main ()
{
  string test = "HAHA hoHOhaha pfiou";

  cout << test << endl;

  string_replace (test, "HAHA", "aa");

  cout << test << endl;
  assert (test == "aa hoHOaa pfiou");

  return 0;
}

Don't hesitate to test it a little before using it daily!
However I think this sould works ok as is.

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top