I have a string, for example; “llama,goat,cow” and I just need to put a ‘@’ in front of each word so my string will look like “@llama,@goat,@cow”, but I need the values to be dynamic also, and always with a ‘@’ at the beginning.
Not knowing a great deal of C++ could someone please help me find the easiest solution to this problem? Many thanks in advance.
I have a string, for example; llama,goat,cow and I just need to put a
Share
Judging by insertable‘s comments, (s?) he’s trying to get this code working… So let me offer my take…
As with the others, I’m presuming each word is delimited by a single “,”. If you can have multiple character delimiters, you’ll need to add a second find (i.e. find_first_not_of) to find the start/end of each word.
And yes, you could insert the ‘@’ characters into the preexisting string. But inserting for each word gets a little inefficient (O(N^2)) unless you’re clever. That sort of cleverness usually comes with a high maintenance/debugging cost. So I’ll just stick to using two strings…
(There ought to be some brilliant way to do this with STL algorithms. But I’m sick and I just don’t see how to accommodate insertion right now…)
References: C++-strings C++-strings STL count_if