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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:14:47+00:00 2026-05-24T07:14:47+00:00

Is there a common C/C++ library (or common technique) for taking a line(s) of

  • 0

Is there a common C/C++ library (or common technique) for taking a line(s) of input text and splitting the words into separate lines. Where each line of output has a max width and words are not split across lines. Whitespace being collapsed or preserved is ok. Punctuation must be preserved. Small and compact library is preferred.

I could easily spend an afternoon putting something together that works, but would like to know if there is something common out there so I don’t re-invent the wheel. Bonus points if the input line can contain a format specifier to indicate an indention level for the output lines.

Example input:
“Shankle drumstick corned beef, chuck turkey chicken pork chop venison beef strip steak cow sausage. Tail short loin shoulder ball tip, jowl drumstick rump. Tail tongue ball tip meatloaf, bresaola short loin tri-tip fatback pork loin sirloin shank flank biltong. Venison short loin andouille.

Example output (target width = 60)

123456789012345678901234567890123456789012345678901234567890   Line added to show where 60 is
Shankle drumstick corned beef, chuck turkey chicken pork
chop venison beef strip steak cow sausage. Tail short loin
shoulder ball tip, jowl drumstick rump. Tail tongue ball tip
meatloaf, bresaola short loin tri-tip fatback pork loin
sirloin shank flank biltong. Venison short loin andouille.
  • 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-24T07:14:47+00:00Added an answer on May 24, 2026 at 7:14 am

    Here’s my approach, it’s certainly not the fastest but I tried to make it as readable as possible. The result is the same as your example.

    #include <iostream>
    #include <string>
    
    
    std::string splitInLines(std::string source, std::size_t width, std::string whitespace = " \t\r")
    {
        std::size_t  currIndex = width - 1;
        std::size_t  sizeToElim;
        while ( currIndex < source.length() )
        {
            currIndex = source.find_last_of(whitespace,currIndex + 1); 
            if (currIndex == std::string::npos)
                break;
            currIndex = source.find_last_not_of(whitespace,currIndex);
            if (currIndex == std::string::npos)
                break;
            sizeToElim = source.find_first_not_of(whitespace,currIndex + 1) - currIndex - 1;
            source.replace( currIndex + 1, sizeToElim , "\n");
            currIndex += (width + 1); //due to the recently inserted "\n"
        }
        return source;
    }
    
    int main() {
        std::string source = "Shankle drumstick corned beef, chuck turkey chicken pork chop venison beef strip steak cow sausage. Tail short loin shoulder ball tip, jowl drumstick rump. Tail tongue ball tip meatloaf, bresaola short loin tri-tip fatback pork loin sirloin shank flank biltong. Venison short loin andouille.";
        std::string result = splitInLines(source , 60);
        std::cout << result;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if there is a common javascript library to enable scrolling that
Is there a standard Java library that handles common file operations such as moving/copying
I have a common library and two executables. Each executable refers to the common
Is there a common library which helps to implement commands entered on a Java
Is there a library intended for compiling/testing your Javascript libraries? I've noticed many common
MyGUI library. There's a line in its sources: mHandle = (size_t)::LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)); mHandle is
There are certain common library functions in erlang that are much slower than their
Are there any Common Lisp implementations for .Net?
Is there a common way to get the current time in or with milliseconds?
Is there a common way to express the usage of arguments in C++? I

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.