Ad
Fundamentals
Strings

The goal of this kumite is to separate each adjacient pair of characters in the original string by an additional space (' ') character.

Original

Used std::accumulate to achieve this

Fork 1

  • if the string is empty, return empty string (next steps require the string to be nonempty)
  • preallocate a buffer of size 2*string.length()-1 filled with space characters
  • map each each character of original string from position i to position 2*i in the buffer
Code
Diff
  • #include <string>
    
    std::string digest(const std::string& str) {
      if(str.empty()) 
        return "";
      std::string buff (str.size()*2-1, ' ');
      for(std::size_t i = 0; i < str.size(); ++i)
        buff[2*i] = str[i];
      return buff;
    }
    • #include <string>
    • #include <numeric>
    • std::string digest(const std::string& param) {
    • return param.empty()?
    • param
    • : std::accumulate(std::next(param.cbegin()),param.cend(),
    • std::string(1,param[0]),
    • [](auto &s, auto c) {return s.append(" ").append(1,c);});
    • std::string digest(const std::string& str) {
    • if(str.empty())
    • return "";
    • std::string buff (str.size()*2-1, ' ');
    • for(std::size_t i = 0; i < str.size(); ++i)
    • buff[2*i] = str[i];
    • return buff;
    • }