need help replaceing words in a string

i am writing a program to replace words in a string
i want the user to input weather they want it  case sensative or not
i also want them to enter if they want the sustings to be replaced or not

anyhelp with this i would appreciate
thanks
ted

#include <iostream>
#include <string>


using namespace std;

int main() {
 
string Sentence = "";                              
string word = "";
string NewWord = "";
char Answer1;
char Answer2;
 
      cout << "Please enter a sentence.:" << endl;
       getline(cin, Sentence);
 
      string Sentencecopy = Sentence;              
                    
      cout << "Enter a word that is to be replaced: ";
      getline(cin,word);

      cout << "Enter the word you want to replace it with: ";
      getline(cin,NewWord);

      Sentence.replace(word.length(), word.length(), NewWord);


size_t start = 0;
 
while(start != string::npos){

   
if(start == 0) {
     
cout << "Case sensitive? (y/n): " << endl;
cin  >> Answer1;
      if(tolower(Answer1) == 'y')


cout << "Replace Substring? (y/n): " << endl;
cin  >> Answer2;
 
}
  return 0;
}}
tedschniedersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Your start was pretty OK, but you'd want to have the users enter the options before you do the search and replace thing:

#include <iostream>
#include <string>


using namespace std;

string ToLower(string& str) {

for_each(str.begin(), str.end(), tolower);

}

void do_replace ( string& strSentence, string& strToReplace, string& strReplaceWith, bool bIgnoreCase, bool bSubstrings) {

string strSentenceLower = strSentence;

if ( bIgnoreCase) {

    ToLower(strToReplace);
    ToLower(strSentenceLower);
}

int n;

if (!bIgnoreCase) n = strSentence.find(strToReplace);
 else n = strSentence.find(strSentenceLower);

if ( -1==n) return; // nothing to replace anyway

if ( strSentence[n - 1] == ' ' && !bSubstrings) return; // not a substring

strSentence.replace(n, strReplaceWith.length(), strReplaceWith);
}

int main() {
 
bool bSubstrings = false;
bool bIgnoreCase = false;
string Sentence = "";                              
string word = "";
string NewWord = "";
char Answer1;
char Answer2;
 
   
cout << "Case sensitive? (y/n): " << endl;
cin  >> Answer1;
    if(Answer1 == 'n') bIgnoreCase = true;


cout << "Replace Substring? (y/n): " << endl;
cin  >> Answer2;
    if(Answer1 == 'y') bSubstrings = true;


 return 0;
}

I'll leve the task to implement the appropriate handling of start/end of string cases when checking for sunbstrings up to you - a lil' bit of homework has to be done. That's BTW why I not tested (even compiled) the above :o)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tedschniedersAuthor Commented:
i cant figgure out how i compare the word in the string they entered to the word they want to replace to see if they are equal
do you have any idea how i would do this
this is what i have so far
thanks,
ted

#include <iostream>
#include <string>


using namespace std;

int main() {
 
string Sentence = "";                              
string word = "";
string NewWord = "";
char Answer1;
char Answer2;
size_t start = 0;
int counter = 0;

      cout << "Please enter a sentence.:" << endl;
             getline(cin, Sentence);
             
      cout << "Enter a word that is to be replaced: ";
            cin >> word;

      cout << "Enter the word you want to replace it with: ";
            cin >> NewWord;

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

      cout << "Replace Substring? (y/n): " << endl;
            cin  >> Answer2;
 
if(tolower(Answer1) == 'n' && tolower(Answer2) == 'n'){
       start = Sentence.find(word);      
           while(start != string::npos){
                 Sentence.replace(start,word.length(),NewWord);
                 start = Sentence.find(word, start + NewWord.length());
      }            
      }

else if(tolower(Answer1) == 'y' && tolower(Answer2) == 'n'){


      

}}



  return 0;
}
0
tedschniedersAuthor Commented:
to clarify to see if they are equal basically case sensative
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.