Take the 2-minute tour ×

I have to create a program that should replace all letters in the first parameter with the second parameter. For example, if the string passed is “How now cow” and the function replaces all ‘o’ to ‘e’ then the new string would be: “Hew new cew.”... I keep getting an error at line 9, the return void part.

#include <iostream>
using namespace std;

string replace(string mystring){
    replace(mystring.begin(), mystring.end(),  'e',  'o');
    return void;
share|improve this question
You might want to reread on how functions work. –  chris Nov 19 '12 at 1:26
What is the error? Is this in a function? Is it in the int main()? Is the error a compile type or a runtime type? Im guessing its because you should be returning an int –  Ben Nov 19 '12 at 1:26
I'm lost, if you could give me a tip to help get me rolling, i'd appreciate it. –  Mike Shamus Nov 19 '12 at 1:26
(expected primary expression before void) (expected ; before void) (declaration does not declare anything) Errors I'm receiving. –  Mike Shamus Nov 19 '12 at 1:30
@Ben he ... what??! –  sehe Nov 19 '12 at 1:30

4 Answers 4

You just need to return the modified string, use return mystring; instead of return void;

share|improve this answer
Also, you technically need #include <algorithm> to pull in std::replace and #include <string> to pull in std::string –  je4d Nov 19 '12 at 1:54
string replace(string mystring){

This function is called replace, takes a string as a parameter and returns a string, this is indicated by the type before the function's name in this prototype.

If it expects you to return a string, you can't return void; because void is not of type string.

So, you'll need to return mystring; instead such that you return a string.

share|improve this answer
ahhhh thank you :) –  Mike Shamus Nov 19 '12 at 1:30
std::replace doesn't return a string, it's void –  je4d Nov 19 '12 at 1:33
@je4d: Indeed, jma127 confused me! Was just looking it up, edited my post before you let me know... ;) –  Tom Wijsman Nov 19 '12 at 1:34

Instead of returning void, do

return mystring;

EDIT: just realized I was talking about wrong language. Sorry everyone.

share|improve this answer
A string is not immutable in C++ since you can tamper with its data; in fact, with C++11 you'll even be able to manipulate it like you would do with a list, for instance pop_back. I haven't downvoted you, so here's another +1 because you've edited it out. –  Tom Wijsman Nov 19 '12 at 1:30
std::replace doesn't return a string, it's void –  Tom Wijsman Nov 19 '12 at 1:36
Apologies again, hard to switch from hours of Python programming back to C++. –  jma127 Nov 19 '12 at 1:45

not very elegant, but it'll get the job done. now you can replace strings with other strings-- or just use strings that are one character long(similar to what you're doing in your example).

#include <iostream>
#include <cstdlib>
#include <string>

std::string string_replace_all( std::string & src, std::string const& target, std::string const& repl){

        if (target.length() == 0) {
                // searching for a match to the empty string will result in                                                                                                                                                                    
                //  an infinite loop                                                                                                                                                                                                           
                //  it might make sense to throw an exception for this case                                                                                                                                                                    
                return src;

        if (src.length() == 0) {
                return src;  // nothing to match against                                                                                                                                                                                       

        size_t idx = 0;

        for (;;) {
                idx = src.find( target, idx);
                if (idx == std::string::npos)  break;

                src.replace( idx, target.length(), repl);
                idx += repl.length();

        return src;

int main(){

    std::string test{"loool lo l l l    l oooo l loo o"};
    std::cout << string_replace_all(test,"o","z") << std::endl;

    return EXIT_SUCCESS;

output: lzzzl lz l l l l zzzz l lzz z

If you're going to use your own implementation, be careful and check your edge cases. Make sure the program won't crash on any empty strings.

share|improve this answer

Your Answer


