3

How do I write a function in C++ that takes a string s and an integer n as input and gives at output a string that has spaces placed every n characters in s?

For example, if the input is s = "abcdefgh" and n = 3 then the output should be "abc def gh"

EDIT:

I could have used loops for this but I am looking for concise and an idiomatic C++ solution (i.e. the one that uses algorithms from STL).

EDIT:

Here's how I would I do it in Scala (which happens to be my primary language):

def drofotize(s: String, n: Int) = s.grouped(n).toSeq.flatMap(_ + " ").mkString

Is this level of conciseness possible with C++? Or do I have to use explicit loops after all?

Daniel
  • 33
  • 4
  • 5
    homework? please tag it as such if that's the case. – Mehrdad Afshari Jun 30 '10 at 20:12
  • 1
    LOL. Homework? I got my degree in 1994. :D It's just that I am new to C++ and thus not acquainted with all its libraries very well. – Daniel Jun 30 '10 at 20:14
  • 1
    Where'd you get it? Toys'R'Us? Sorry, couldn't resist. Seriously though, I'd add a little more, like are we talking STL strings here, or plain old null terminated char arrays? – Paul Dixon Jun 30 '10 at 20:16
  • Even people with an academic degree can study further and thus actually get homeworks. – PeterK Jun 30 '10 at 20:16
  • 2
    @Paul, STL strings, of course. And I want STL solutions only. (The language tag reads C++, not C.) – Daniel Jun 30 '10 at 20:27
  • @PeterK: Homework of this sort?! – Daniel Jun 30 '10 at 20:27
  • @Daniel: There is std::string.insert(), std::string.substring(). Also I think you'll find this interesting: http://stackoverflow.com/questions/236129/c-how-to-split-a-string – SigTerm Jun 30 '10 at 20:51
  • Oh thanks @SigTerm, that looks interesting :) – Daniel Jun 30 '10 at 20:54
  • @Daniel: doing a quick search on homework here on SO will reveal much stranger homeworks ;) – PeterK Jun 30 '10 at 21:12

2 Answers2

2

Copy each character in a loop and when i>0 && i%(n+1)==0 add extra space in the destination string.


As for Standard Library you could write your own std::back_inserter which will add extra spaces and then you could use it as follows:

std::copy( str1.begin(), str1.end(), my_back_inserter(str2, n) );

but I could say that writing such a functor is just a wasting of your time. It is much simpler to write a function copy_with_spaces with an old good for-loop in it.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • Are you kidding me? Do you really think I can't even develop a looping logic? I want a more idiomatic (read: the one that uses STL) solution and that's why I posted the question here. – Daniel Jun 30 '10 at 20:26
  • @Daniel, you didn't even mentioned about STL in your original question. What are you really looking for? Could you be more specific in your question? – Kirill V. Lyadvinsky Jun 30 '10 at 20:35
  • This is the kind of answer I was looking for. Thanks. :) (Just add an EDIT after your first paragraph so that people know what's new and what was there in the original post.) – Daniel Jun 30 '10 at 20:52
2

STL algorithms don't really provide anything like this. Best I can think of:

#include <string>
using namespace std;

string drofotize(const string &s, size_t n)
{
    if (s.size() <= n)
    {
        return s;
    }
    return s.substr(0,n) + " " + drofotize(s.substr(n), n);
}
Ben Straub
  • 5,675
  • 3
  • 28
  • 43