-2

My dylan::Replace() function supposed to take a string as the parameter, replace the spaces with asterisks ('*') and return a string. The code below is what I have:

#define n 0
namespace dylan{
    string Replace(string);

    string Replace (string s){
        if (s.substr(n)=="")
            cout << "string is empty \n";
        else
            s.replace(s.begin(),s.end(),' ','*');
        return s;
    }
}

using namespace dylan;

int main (int argc, char * argv[]){
    string s="a b c";
    string sAfter=Replace(s);
    // some more stuff
}

But G++ tells me that within dylan::Replace() there is no matching function for call to std::basic_string<CharT,_Traits,_Alloc>replace(...)

BONUS POINT: are there any recursive ways to do the same job (replace a certain character in a string)?


QUESTION UPDATED: I modified and ran the program and it doe not do the job I wanted. Instead it printed asterisks multiple times.

Dylan Czenski
  • 1,305
  • 4
  • 29
  • 49
  • 2
    Replace `s.end` with `s.end()` That will fix the compilation error, but still won't do what you want. Read the documentation for [`string::replace`](http://en.cppreference.com/w/cpp/string/basic_string/replace). There's no overload that matches what you want it to do. – Praetorian Nov 10 '15 at 01:06
  • 1
    you used `s.end` where you seemed to intend `s.end()` – JSF Nov 10 '15 at 01:06
  • @vishal `#define n 0` – Dylan Czenski Nov 10 '15 at 01:06
  • @vishal: `#define n 0`, which is a really bad idea. – user2357112 Nov 10 '15 at 01:06
  • @user2357112 why is it? I am new to this, please explain. Appreciate it! – Dylan Czenski Nov 10 '15 at 01:08
  • @Praetorian thank you! I was so careless! Do you know the answer of my bonus point question? – Dylan Czenski Nov 10 '15 at 01:10
  • 3
    Any code anywhere in your file that wants to use the name `n` for anything whatsoever is going to have it replaced by `0`. It causes weird surprises and forbids all other code in the file from using a fairly common variable name. – user2357112 Nov 10 '15 at 01:12
  • 4
    @DylanChen [Why are preprocessor macros evil and what are the alternatives?](http://stackoverflow.com/q/14041453/3425536) – Emil Laine Nov 10 '15 at 01:17
  • 2
    `std::string` has an `empty()` function, by the way, that returns true if the string is empty and false if not. There's also a free function `std::replace` that works for replacing characters in a string. – chris Nov 10 '15 at 01:17
  • @zenith thank you! Very informative – Dylan Czenski Nov 10 '15 at 01:40
  • @πάνταῥεῖ, I wouldn't take them so literally. I don't mind the concept of bonus points as long as they are purely a logical extension to the question such that someone could make an answer more complete (while still relevant) by addressing that and still have a great answer if they don't. – chris Nov 10 '15 at 02:03
  • @chris Well, I don't mind so much either, if this were a great question, which it isn't actually :-P ... – πάντα ῥεῖ Nov 10 '15 at 02:05

1 Answers1

1

But G++ tells me that within dylan::Replace() there is no matching function for >call to std::basic_stringreplace(...)

std::string::replace take range of position in string and new content as input. It can not replace one character with another, the possible solution is std::replace_copy:

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

namespace dylan{
    std::string Replace(const std::string &s){
        std::string res(s.size(), ' ');
        std::replace_copy(s.begin(), s.end(), res.begin(),
                  ' ', '*');
        return res;
    }
}

int main()
{
    std::string s="a b c";
    std::string sAfter = dylan::Replace(s);
    std::cout << sAfter << "\n";
}

recursive variant:

std::string ReplaceReq(std::string::const_iterator it, std::string::const_iterator end)
    {
        std::string res;
        if (it == end)
            return res;
        res += *it == ' ' ? '*' : *it;
        return res + ReplaceReq(it + 1, end);
    }

usage:

std::string sAfter2 = dylan::ReplaceReq(s.begin(), s.end());
std::cout << sAfter2 << "\n";
fghj
  • 8,898
  • 4
  • 28
  • 56