String Replacer

Posted on 2006-05-01
Last Modified: 2010-04-01
I am trying to write a program that will replace the word he in a string with she or he this is my code soo far and I am stuck on if I am going in the right direction or not... or what I should do from here if I am going in the right direction:

#include <iostream>
using namespace std;

string::size_type = unsigned int u[npos]
string str1 = "school";
      if (pos==npos)
void replace(string&stmt, string source, string test)

int main (){
cin >> getline.stmt;
replace (stmt, "he", "she or he");
replace (stmt, "him", "her or him");
replace (stmt, "his", "her(s) or his");

start loop
Question by:Geezy1984
    LVL 15

    Accepted Solution

    I suggest you try to solve simpler problems first and gradually work your way up to this one.  For example, first make a program that can read in a string.  Then improve it so it can find a specified string in an input string.  Then improve it so it can find all occurrences of a specified string in the input string.  Finally, make it do replacements.  Get it to compile and test it after each step before going on the next one.
    LVL 39

    Assisted Solution

    >>>> string::size_type = unsigned int u[npos]


    >>>> string::size_type

    ??? There is no need to play with basic_string::size_type

    >>>> pos=str1.find("he")
    >>>> pos=str1.find("him")

    The second statement overwrites the value of pos ???

    >>>> void replace(string&stmt, string source, string test)

    There is already a std::string::replace and I don't think it is a good idea to have a global function of the same name but with different arguments. Better use a different name and return the new string by value (rather than passing a writeable string variable).

    string replaceString(const string& text, const string& strold, const string& strnew);

    If the function should replace all occurences of the old string found you should name it 'replaceAllString' to make that clear.

    For implementation you need to call the string::find (if replacing all occurences, call it in a loop). Then use the pos returned as first argument to string::replace. The second argument is the length of the string you want to replace. The thirs argument is the string you want take for replace.

    Note, you need to check the return of 'find' not equal to string::npos or break. If replacing all occurences you need to increment the position returned by find by the length of the new string before continuing the loop.

    >>>> start loop

    That function is not needed.

    Regards, Alex


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now