Insert the infront character of each word in a line using C ++

I have a string, for example; “llama, goat, cow,” and I just need to put an “@” in front of every word so that my line looks like “@llama, @goat, @cow,” but I need values ​​that will also be dynamic, and always with “ @" initially. Without knowing much C ++, can someone help me find the easiest solution to this problem? Thank you very much in advance.

+3
source share
3 answers

Judging by the inserted comments, (s?) It is trying to get this code to work ... So let me suggest my trick ...

Like others, I assume that each word is limited to one ",". If you can have multiple character delimiters, you need to add a second search (e.g. find_first_not_of) to find the beginning / end of each word.

And yes, you can insert the characters '@' into an existing line. But inserting for each word becomes a little ineffective (O (N ^ 2)) if you are not smart. Such a mind usually comes with a high maintenance / debugging cost. So I will just stick to two lines ...

(There must be some brilliant way to do this using STL algorithms, but I'm sick and I just don't understand how to place the insert right now ...)

References: C ++ -   C ++ strings -   STL   count_if strings

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

#define SHOW(X)  cout << # X " = " << (X) << endl

int main()
{
             //     0123456789_123456789_1234
  string  inString(",llama,goat,cow,,dog,cat");
  string  outString;

/* This code assumes inString.size() > 0 */

  const iterator_traits<string::iterator>::difference_type  numberOfWords
    = count_if( inString.begin(), inString.end(),
                bind2nd( equal_to<char>(), ',' ) )
       + 1;

  string::size_type  startIndex, endIndex;


  outString.reserve( inString.length() + numberOfWords );


  for ( startIndex = endIndex = 0;
        endIndex != string::npos;
        startIndex = endIndex + 1 )
  {
    outString += "@";

      /* No startIndex+1 here.  We set startIndex=endIndex+1 in the for loop */
    endIndex = inString . find_first_of( ",", startIndex );

    outString . append ( inString, startIndex,
                         (   (endIndex == string::npos)
                           ? string::npos : endIndex - startIndex + 1) );
  }

  SHOW( numberOfWords );
  SHOW( inString );
  SHOW( outString );
  SHOW( inString.size() );
  SHOW( outString.size() );
  SHOW( inString.capacity() );
  SHOW( outString.capacity() );
}
+1
source

, . , - , , :

// warning: untested.
std::string input("llama,goat,cow");
std::ostringstream o;

std::string word;

std::istringstream i(input);
while (std::getline(input, word, ','))
    o << "@" << word << ",";

std::string result(o.str(), o.str().size()-1);

// show the result:
std::cout << result;

: getline: iostream "" char. - , std::string. , . , #include <string>.

+4

++:

basic_string : basic_string ('string' basic_string) find_first_of(), , . append (+ =), , "@" .

C-:

You can start with strtok_s

Which will “tokenize” the string by searching for word break delimiters such as commas or spaces. Then you can copy the parts between the delimiters to another buffer, and then put the "@" characters between them when you go along

For this, I would use strcpy_s to copy a piece into a piece into a new buffer.

+2
source

Source: https://habr.com/ru/post/1738742/


All Articles