replace occurences in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • outofmymind
    New Member
    • Oct 2006
    • 45

    replace occurences in a string

    hey,

    i have to use the commands find and replace to replace all the occurrences of sydney to melbourne.

    I have did this, but not sure if im on the right track:

    Code:
    #include <string.h>
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
      
    string s1="sydney";
    cout << s1.replace(1,6,"melbourne",9);
    
        getch();
        return 0;
    
    }
    can anyone tell me if i solved this correctly,
    thankss
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by outofmymind
    hey,

    i have to use the commands find and replace to replace all the occurrences of sydney to melbourne.

    I have did this, but not sure if im on the right track:

    Code:
    #include <string.h>
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
      
    string s1="sydney";
    cout << s1.replace(1,6,"melbourne",9);
    
        getch();
        return 0;
    
    }
    can anyone tell me if i solved this correctly,
    thankss
    not quite, in C/C++ array indicies start from 0 so it should be
    Code:
    cout << s1.replace(0,6,"melbourne",9);
    see the following page for details of replace()
    http://www.cppreferenc e.com/cppstring/replace.html

    Comment

    Working...