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 6841095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:55:28+00:00 2026-05-26T23:55:28+00:00

In C++, how do I split a string into evenly-sized smaller string? For example,

  • 0

In C++, how do I split a string into evenly-sized smaller string?

For example, I have a string “012345678” and want it to split it into 5 smaller strings, and this should return me something like “01”, “23”, “45”, “67”, “8”.

I’m having trouble of determining the length of the smaller strings. In the previous example, the original string is size 9, and I want to split it into 5 smaller string, so each smaller string except the last one should be length 9 / 5 = 1, but then the last one will be length 9 – 1* 4 = 5, which is unacceptable.

So the formal definition of this problem: the original string is split into EXACTLY n substrings, and no two of the substrings should differ by greater than 1 in length.

My focus is not on C++ syntax or library. It’s how to design an algorithm so that the returned string can be nearly-equal in size.

  • 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-26T23:55:28+00:00Added an answer on May 26, 2026 at 11:55 pm

    To divide N items into M parts, with lengths within one unit, you can use formula (N*i+N)/M - (N*i)/M as length of i‘th part, as illustrated below.

     #include <string>
     #include <iostream>
     using namespace std;
    
     int main() {
       string text = "abcdefghijklmnopqrstuvwxyz";
       int N = text.length();
       for (int M=3; M<14; ++M) {
         cout <<" length:"<< N <<"  parts:"<< M << "\n";
         int at, pre=0, i;
         for (pre = i = 0; i < M; ++i) {
           at = (N+N*i)/M;
           cout << "part " << i << "\t" << pre << "\t" << at;
           cout << "\t" << text.substr(pre, at-pre) << "\n";
           pre = at;
         }
       }
       return 0;
     } 
    

    For example, when M is 4 or 5, the code above produces:

      length:26  parts:4
     part 0 0   6   abcdef
     part 1 6   13  ghijklm
     part 2 13  19  nopqrs
     part 3 19  26  tuvwxyz
      length:26  parts:5
     part 0 0   5   abcde
     part 1 5   10  fghij
     part 2 10  15  klmno
     part 3 15  20  pqrst
     part 4 20  26  uvwxyz
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Split String into smaller Strings by length variable I have seen solutions
I have a string 10/15/2010 I want to split this string into 10, 15,
I have a string ABCD:10,20,,40;1/1;1/2,1/3,1/4 I want to split the string into the following
How would I split a comma delimited string into smaller comma delimited strings? My
I have this code to split a string into groups of 3 bytes: str=hello
I want to split a string into N number of 2char strings. I know
Really simple problem: I want to split a connection string into its keyword /
I want to split a string like 001A into 001 and A
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I have to split a string into several ones. In fact what i need

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.