Solved

need help replaceing words in a string

Posted on 2004-10-31
210 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
Question by:tedschnieders
    3 Comments
     
    LVL 86

    Accepted Solution

    by:
    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
    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
    to clarify to see if they are equal basically case sensative
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now