Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 702261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:42:04+00:00 2026-05-14T03:42:04+00:00

I have a string, for example; llama,goat,cow and I just need to put a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:42:04+00:00Added an answer on May 14, 2026 at 3:42 am

    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

    #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() );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string variable that represents the name of a custom class. Example:
Suppose I have a string 'nvarchar(50)', which is for example the T-SQL string segment
Say for example I have the following string: var testString = Hello, world; And
For example, if I have a unicode string, I can encode it as an
For example, I have an ASP.NET form that is called by another aspx: string
For example, suppose I have a class: class Foo { public: std::string& Name() {
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have a string coming from a table like can no pay{1},as your payment{2}due
Ok, I have a string of the form string temp = http://www.example.com?file=666111&submitter=Betty&origin=Office&telNo=05555; what I
I have a std::string with multiple lines and I need to read it line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.