[Dev-C++] Search and Replace - Take 2 - Sent as Plain Text
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: CARLSON, J. M. <CA...@st...> - 2003-06-26 06:37:55
|
// Search and Replace Function // Praise God for His Goodness and Mercy
// 22 June 2003 // Jay A Carlson
// Search and Replace Implementation and Testing
//
// Takes an input text and replacement text and makes it happen
// In the future I will have an input text file and a change file
// to end up with a new converted file.
//
// The specifications take the form of
// start delimiter, stop delimiter
// start and stop delimiters must be different although not check (yet)
// search and replace words inside of the start and stop delimiters
// search - can be any text, number, word, or a phrase
// replace - can be any text, number, word, or phrase
// examples are as below:
// <> <search for this> <replace with this>;
// [] [search] [replace]
// <> <myArray[9]> <thisArray[10]>
// ^* ^std::cout<<test[5];* ^printf("test[3]");*
//
// Notice by being able to specify the start and stop delimiters for each
// search and replace you don't have to worry about nesting problems.
//
// abbreviations
// srt -- search and replace text
//
// personal notes: I did this for two reasons,
// 1. To learn how to manipulate C++ std::strings, and I am now just
getting
// the hang of it.
// 2. I actually need this for a specific purpose
//
// It could be used to replace >>>>>>> on email with ' ' or "".
//
// I am posting this on the internet for new people so they won't have to
// try and figure this all out from scratch. It took me a long time
and
// in general I know how to program but the std::library stuff is
taking
// me longer than I thought to learn.
//
// A REAL programmer probably wouldn't comment any of this because it is
// probably obvious. I am going to comment the heck out of it to help
// new people and maybe myself.
//
// Because there are so many comments you might have to save two copies,
// one with the comments, and one with a lot of the comments stripped
out.
#include <iostream> // for std::cout
#include <string> // for std::string
#include <vector> // guess what - for vectors
// this is a function whose sole purpose in life is to get the word, or
words
// between the delimiters represented by the variables start and stop
// srt -- search and replace text
// debug -- a way of showing more or less information in a hurry
std::string getword_s(char start, char stop, std::string &srt, const bool
debug)
{
// \n - means put in a new line
// srt - same as above
if (debug) std::cout << "\nsrt = " << srt;
// I used unsigned int for first, last and len,
// I may have been able to use std::string::iterator instead
// but why would I want to type even more than I am now.
// I am sure for sorting iterators would be the way to go.
unsigned int first = srt.find_first_of(start) + 1;
unsigned int last = srt.find_first_of(stop) - 1;
unsigned int len = last - first + 1; // length of word(s) between
delimiters
// get the substring of srt, starting at position first (delimeter +1)
and
// go for len charachers. If len is larger than the string srt it
will
// just stop at the end.
std::string word = srt.substr(first, len);
// now that we got the word(s) we wanted out between the delimiters, it
is
// time to remove that section so that we can work with the next part, if
// any. Once again the substring will stop at the end of the string,
// regaurdless of where length() tells it to stop.
srt = srt.substr(last + 2, srt.length());
// just so we can monitor to see that everything is working ok
if (debug) std::cout << "\nword(s) = " << word;
return word;
}
// the name says it all
void search_and_replace(std::string &text, std::string &srt, const bool
debug) {
// I didn't want an accidental space to cause the program to glitch
// so in the next few lines I remove all spaces before delimiters.
// This also simplfied getting the delimiters
unsigned int eatwhite = srt.find_first_not_of(' ');
srt = srt.substr(eatwhite,srt.length());
char start = srt[0]; // the starting delimiter is now the first char in
srt
srt = srt.substr(1,srt.length());
eatwhite = srt.find_first_not_of(' ');
srt = srt.substr(eatwhite,srt.length());
char stop = srt[0]; // the ending delimiter is now the first char in
srt
// C++ uses 0 to start arrays and strings. It makes it easy to make FOR
// loops for(int i = 0; i < length; i++) {code here}
// You never want to end a loop on an i == length there are two many
times
// when they don't match perfectly and you run the risk of going into
// an infinite loop. (Okay maybe not so much for an integer, but a
float
// or double has a good chance that won't match perfectly. So it is
good
// programming practice to just use '<' instead of '==' .
// Knowing that if you started the array at 1 you would
// have for(int i=1; i < length+1; i++) { code here } doesn't look to
bad,
// but when you get into multidimensional arrays, it gets harder to get
// the index number if you don't start at zero.
// Just learn to be standard...
if (debug) {
// Once again, if we need to see if the output is correct.
std::cout << "\nStart Delimiter = " << start;
std::cout << "\nStop Delimiter = " << stop;
}
// remove the delimiter specifiers so we can look for the search
// and replace words
srt = srt.substr(2, srt.length()-1);
if (debug) std::cout << "\nsrt = " << srt << "\n";
// call the function getword_s to get the word(s) for the search part
std::string search = getword_s(start, stop, srt, debug);
if (debug) std::cout << "\n";
// call the function getword_s to get the word(s) for the replace part
std::string replace = getword_s(start, stop, srt, debug);
if (debug) {
std::cout << "\n\nText: \n" << text;
std::cout << "\n\nThe word(s) to search for is/are : \"" << search <<
"\"";
std::cout << "\nThe word(s) to replace with is/are: \"" << replace <<
"\"";
std::cout << "\n";
}
// You better check to see that what your searching for is there before
// you try to replace it. If it is not there you can't replace it,
and
// your program will terminate before you can view any std::cout data.
if (text.length() > text.find(search)) {
//THIS IS IT - THE WHOLE PROGRAM IS MADE FOR THIS LINE TO DO ITS WORK
// Find the search word(s) in the text, and replace it with the word(s)
// stored in the variable replace, (not to be confused with the call of
the
// function text.replace()) for a length of the original word(s).
// Note the string will be expanded as necessary for the replacement to
fit.
// If the search word is not found, it cannot be replaced!!
text = text.replace(text.find(search), search.length(), replace);
}
std::cout << "\n";
}
int main(void) {
bool debug = false; // toggle to see more details
std::string original = "I will be 40 next year.";
std::string new_text = original;
// Oh yeah - here I am experimenting with vectors of strings
// Vectors are almost like arrays, but better
std::vector<std::string>srt(4); // srt - search and replace text
// Test no match condition
srt[0] = " < > <35> <34>";
// Test replace with same size
srt[1] = " ( ) (40) (38)";
// Test replace with smaller size
srt[2] = "[ ] [ 38] []";
// Test replace with a bigger text
srt[3] = "[] [next] [a very old man next]";
for(int i = 0; i < 4; i++) {
std::cout << "\nsrt = " << srt[i];
search_and_replace(new_text, srt[i], debug);
std::cout << "\nOriginal text: " << original;
std::cout << "\nNew text: " << new_text;
if (debug) {
std::cout << "\n\n\n\n";
system("pause");
}
else std::cout << "\n\n";
original = new_text;
}
if (!(debug)) system("pause");
return 0;
}
// I hoped you like the way I use debug to show whats going on
// I am finally getting the hang of the STL .. well a little bit anyway.
// It took me forever to get some of the syntax right, but now learning can
// happen. And if its too late to learn there is alway cut and paste.
// Praise God for His Goodness and Mercy
// Jay A. Carlson
|