简体   繁体   中英

Function to replace specific letters won't run with void return type

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;
}

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

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 .

Instead of returning void, do

replace(mystring.begin(), mystring.end(),  'e',  'o');
return mystring;

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

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 llll 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM