codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Substring replacement problem... help pls!  JackM at 17:33 on Wednesday, November 08, 2006
 

ok, so I need to write a program that scans a piece of text and performs a replace funtion. The program should promt the user to enter some text; spaces are ok. It should then prompt the user to enter the word that needs to be replaced, followed by the word that should replace the previous word. The program then needs to know if it should ignore case and replace substrings. Once all the info is gathered the text should have all matching strings or substrings replaced.

the problem Im having is with the substring replacement parts... its just not working, here is what I have:



#include<iostream>
#include<string>
#include<cctype>
#include<cstring>
#include<ctype.h>

using namespace std;

//funtion to convert to upper
string StringToUpper(string strToConvert)
{
for(unsigned int i=0; i < strToConvert.length(); i++)
{
strToConvert = toupper(strToConvert);
}
return strToConvert;
}

int main()
{
string sentance; //yeah yeah, i spelled sentence wrong, I know...
string lsentance;
string replacement;
string error;
string lerror;
string fixer = "xxx";
char sensitive = 0;
char substrings = 0;
int start = 0;
int subs = 0;



cout << "Enter text: "<<endl;
getline(cin, sentance);
lsentance = sentance;

cout << "Enter word to be replaced: " <<endl;
cin >> error;
lerror = error;

cout<< "Enter replacement word: "<<endl;
cin >> replacement;

cout<< "Case sensitive? (y/n): "<<endl;
cin >> sensitive;

cout<<"Replace substrings? (y/n): "<<endl;
cin >> substrings;

//case sensitive (works)
if(sensitive == 'y' && substrings == 'y'){
if(sentance.find(error) == string::npos)
cout<<"Word not found!"<<endl;
else{
start = sentance.find(error);
int length = error.length();
do{
sentance.replace(start, length, replacement);
start = sentance.find(error);
}while(start != string::npos);
}}

//case insensitive (works)
if(sensitive == 'n' && substrings == 'y'){
if((StringToUpper(lsentance)).find(StringToUpper(lerror)) == string::npos)
cout<<"Word not found!"<<endl;
else{
start = (StringToUpper(lsentance)).find(StringToUpper(lerror));
int length = error.length();
do{
sentance.replace(start, length, replacement);
lsentance.replace(start, length, replacement);
start = (StringToUpper(lsentance)).find(StringToUpper(lerror));
}while(start != string::npos);
}}

//doesnt look at substrings, case sensitive (no dice)
if(sensitive == 'y' && substrings == 'n'){
if(sentance.find(error) == string::npos)
cout<<"Word not found!"<<endl;

else{
start = sentance.find(error);

int length = error.length();

do{ //change all "errors" to "replacement"
if(isalpha(sentance.find(start - 1 ,1) == 0) && isalpha(sentance.find(start + error.length() ,1) == 0)){
sentance.replace(start, length, replacement);
start = sentance.find(error);}
else{//change start to fixer as to satisy the while condition
sentance.replace(start, length, fixer);
start = sentance.find(error);}

}while(start != string::npos);
//change all of the fixers back to errors
subs = sentance.find(fixer);
}do{
sentance.replace(subs, 3, error);
subs = sentance.find(fixer);
}while(subs != string::npos);
}


//doesnt look at substrings, case insensitive (hf gl...)
if(sensitive == 'n' && substrings == 'n'){
if((StringToUpper(lsentance)).find(StringToUpper(lerror)) == string::npos)
cout<<"Word not found!"<<endl;

else{
start = (StringToUpper(lsentance)).find(StringToUpper(lerror));

int length = error.length();

do{ //change all "errors" to "replacement"
if(isalpha(StringToUpper(lsentance).find(start - 1 ,1) == 0) && isalpha(StringToUpper(lsentance).find(start + error.length(),1) == 0)){
lsentance.replace(start, length, replacement);
sentance.replace(start, length, replacement);
start = (StringToUpper(lsentance)).find(StringToUpper(lerror));}
else{//change start to fixer as to satisy the while condition
sentance.replace(start, length, fixer);
lsentance.replace(start, length, fixer);}
start = (StringToUpper(lsentance)).find(StringToUpper(lerror));
}while(start != string::npos);
//change all of the fixers back to errors
subs = sentance.find(fixer);
}do{
sentance.replace(subs, 3, error);
subs = sentance.find(fixer);
}while(subs != string::npos);
}


//deals with invalid responses
if(sensitive != 'n' && sensitive != 'y')
cout<<"invalid response"<<endl;

if(substrings != 'n' && substrings != 'y')
cout<<"invalid response"<<endl;


//print the sentence
cout<< sentance <<endl;
return 0;
}




Any help would be greatly appreciated! Thx!









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Run Time Error
•  jsp login
•  Form SELECT auto post or get of selection
•  Re: Data sharing in java thread()
•  how to capture screen from a webpage using javascript
•  How to place the blinking cursor in a particular position in a text box?
•  kjhkhgkkkkkkkkkkkkkkkkkkkkkkkkkkkk
•  kjhkhgkkkkkkkkkkkkkkkkkkkkkkkkkkkk
•  kjhkhgkkkkkkkkkkkkkkkkkkkkkkkkkkkk


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2006