?
Solved

need help replaceing words in a string

Posted on 2004-10-31
3
Medium Priority
?
213 Views
Last Modified: 2010-04-01
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;
}}
0
Comment
Question by:tedschnieders
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1500 total points
ID: 12460160
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
 

Author Comment

by:tedschnieders
ID: 12467187
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
 

Author Comment

by:tedschnieders
ID: 12467209
to clarify to see if they are equal basically case sensative
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question