#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Having trouble with replacing one char


    I am having trouble with trying to replace one char in c++ string for another one I need to change all the e to s in this code can someone help thank you, I am sorry if I posted this wrong this is my first time doing something like this please someone answer my question thank you


    #include <iostream>
    #include <string>
    #include <algorithm>


    using namespace std;

    string replace(string funPhrase, char e, char s);

    int main()
    {
    string funPhrase = "Programming is fun to learn, even better when mastered!";
    char e = 'e';
    char s = 's';

    cout << funPhrase << endl;
    cout << endl;
    cout << replace(funPhrase, e, s) << endl;
    cout << endl;
    system("pause");
    return 0;
    }

    string replace(string funPhrase, char e, char s)
    {
    int i;
    int length = 0;

    for(i = 0; i < length; i++)
    {
    if(funPhrase[i] == 'e')
    {
    funPhrase[i] = 's';
    }


    }






    return funPhrase;
    }
    Last edited by bojomyjaas; November 8th, 2012 at 08:12 PM. Reason: to let people know this is my first time here and don't know if I posted wrong cause no
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by bojomyjaas
    I am having trouble with trying to replace one char in c++ string for another one I need to change all the e to s in this code can someone help thank you


    #include <iostream>
    #include <string>
    #include <algorithm>


    using namespace std;

    string replace(string funPhrase, char e, char s);

    int main()
    {
    string funPhrase = "Programming is fun to learn, even better when mastered!";
    char e = 'e';
    char s = 's';

    cout << funPhrase << endl;
    cout << endl;
    cout << replace(funPhrase, e, s) << endl;
    cout << endl;
    system("pause");
    return 0;
    }

    string replace(string funPhrase, char e, char s)
    {
    int i;
    int length = 0;

    for(i = 0; i < length; i++)
    {
    if(funPhrase[i] == 'e')
    {
    funPhrase[i] = 's';
    }


    }






    return funPhrase;
    }
    Try passing your string as a reference in your function replace(). Also

    Code:
    int length = 0;
    
    for(i = 0; i < length; i++)
    /*...*/
    Your for loop doesn't loop because length = 0.
    Last edited by G4143; November 8th, 2012 at 08:16 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by G4143
    Try passing your string as a reference in your function replace().


    Thanks I tried that and it didn't help
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by bojomyjaas
    Thanks I tried that and it didn't help
    I would check my updated advice.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by G4143
    Try passing your string as a reference in your function replace(). Also

    Code:
    int length = 0;
    
    for(i = 0; i < length; i++)
    /*...*/
    Your for loop doesn't loop because length = 0.
    If I just put int length; I get a break telling me my length The variable 'length' is being used without being initialized. so I need that 0 there right?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by bojomyjaas
    If I just put int length; I get a break telling me my length The variable 'length' is being used without being initialized. so I need that 0 there right?
    Try something like...

    Code:
    for (int i = 0; i < funPhrase.length(); ++i)
    {
    /*...*/
    }
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by G4143
    Try something like...

    Code:
    for (int i = 0; i < funPhrase.length(); ++i)
    {
    /*...*/
    }

    Thank you so very much that worked I appreciate your help :)

IMN logo majestic logo threadwatch logo seochat tools logo