[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

need help replaceing words in a string

Posted on 2004-10-31
3
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

656 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