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

  • Home
  • SEARCH
  • 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 6673253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:36:21+00:00 2026-05-26T03:36:21+00:00

I am trying to write a function that will find any even subsets of

  • 0

I am trying to write a function that will find any even subsets of characters in a string, and break them in half by inserting an empty space into the string. For example, given the string “AA BBB CCDD”, my string should be converted to “A A BBB CC DD”.

My breakString() function doesn’t seem to work, and I have no idea why. Can anyone help?

void breakString(string &str1)
{
    int pos1 = -1;
    int pos2 = str1.find_first_of(" ", pos1+1);

    while (pos2 != -1)
    {
        if(((pos2-pos1)-1)%2 == 0)
        {
            str1.insert((pos2-pos1)/2, 1, ' ');
            return;
        }
        else
        {
            pos1 = pos2;
            pos2 = str1.find_first_of(' ', pos1+1);
        }
    }

    if((str1.size() - pos1)%2 == 1)     
        str1.insert((str1.length()-pos1)/2, 1, ' ');

    return;
}
  • 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-26T03:36:21+00:00Added an answer on May 26, 2026 at 3:36 am

    You say it doesn’t work, but you don’t say how. I can see two immediate
    prblems: the first is that you return immediatly when you find the first
    word with an even length, so you obviously won’t break any following
    words; and the second is that if you do want to continue, the insert
    will have changed the values of any position after it.

    (FWIW: I’d probably break the entire string down into words first,
    putting them in an std::vector<std::string>, then iterate on that,
    inserting the extra space where needed, then reassemble them. It’s
    probably a bit slower than the approach you’re using, but it’s a lot
    cleaner and easier to follow.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a function that will pull the name of a
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I'm trying to write a regex function that will identify and replace a single
I am trying to write a JavaScript function that will return its first argument(function)
I'm trying to write a custom function that will let me retrieve a cell
I'm trying to write a short function that will let me quickly read in
I'm trying to write a regular expression (via Autohotkey's RegExReplace function) that will enforce
I am trying to write a javascript function with an if statement that will
I am trying to write a filter function for my application that will take

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.