hi again..
i made program again..but still makes error..i really dont know..why..
can you guys tell me where...thanks so much
#include<iostream.h>
#include<string.h>
using namespace std;
void search_and_replace(char *text, char *s, char *r);
int main()
{
const char text[256] = "You can find and replace any part of this line using this program.";
char copy[256];
strcpy(copy, text);
// Search demo
cout << "Original test is ..." << endl;
cout << text << endl << endl;
cout << "Searching..." << endl;
search_and_replace(copy, "find", "search");
cout << copy << endl;
strcpy(copy, text);
cout << "Searching..." << endl;
search_and_replace(copy, "and", "&");
cout << copy << endl;
strcpy(copy, text);
cout << "Searching..." << endl;
search_and_replace(copy, "this line", "a sentence");
cout << copy << endl;
strcpy(copy, text);
cout << "Searching..." << endl;
search_and_replace(copy, "this", "the");
cout << copy << endl;
return 0;
}
void search_and_replace(char *text, char *s, char *r)
{
string strText = text;
string search = s;
string replace = r;
cout << "Replacing..." << endl;
basic_string <char>::size_type index;
index = strText.find(search);
// If search text is not found
if ( index == string::npos )
{
cout << "No search text found." << endl;
return;
}
// Delete search text
strText.erase(index, search.length());
// Insert replace text
strText.insert(index, replace);
// Copy to text
strcpy(text, strText.c_str());
}