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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:33:02+00:00 2026-05-23T09:33:02+00:00

Does anyone know a good algorithm to word wrap an input string to a

  • 0

Does anyone know a good algorithm to word wrap an input string to a specified number of lines rather than a set width. Basically to achieve the minimum width for X lines.

e.g. "I would like to be wrapped into two lines"
goes to
"I would like to be
wrapped into two lines"

"I would like to be wrapped into three lines"
goes to
"I would like to
be wrapped into
three lines"

Inserting new lines as required. I can find other word wrap questions but they all have a known width and want to insert as many lines as needed to fit that width. I am after the opposite.

Answers preferable in a .NET language but any language would be helpful. Obviously if there is a framework way to do this I am not aware of let me know.

Edit I have found this since which I think the accepted answer is the solution to my problem but am having difficulty understanding it. Algorithm to divide text into 3 evenly-sized groups any chance someone could convert it to c# or vb.net.

  • 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-23T09:33:03+00:00Added an answer on May 23, 2026 at 9:33 am

    Here is the accepted solution from Algorithm to divide text into 3 evenly-sized groups converted to C#:

    static List<string> Minragged(string text, int n = 3)
    {
        var words = text.Split();
    
        var cumwordwidth = new List<int>();
        cumwordwidth.Add(0);
    
        foreach (var word in words)
            cumwordwidth.Add(cumwordwidth[cumwordwidth.Count - 1] + word.Length);
    
        var totalwidth = cumwordwidth[cumwordwidth.Count - 1] + words.Length - 1;
    
        var linewidth = (double)(totalwidth - (n - 1)) / n;
    
        var cost = new Func<int, int, double>((i, j) =>
        {
            var actuallinewidth = Math.Max(j - i - 1, 0) + (cumwordwidth[j] - cumwordwidth[i]);
            return (linewidth - actuallinewidth) * (linewidth - actuallinewidth);
        });
    
        var best = new List<List<Tuple<double, int>>>();
    
        var tmp = new List<Tuple<double, int>>();
        best.Add(tmp);
        tmp.Add(new Tuple<double, int>(0.0f, -1));
        foreach (var word in words)
            tmp.Add(new Tuple<double, int>(double.MaxValue, -1));
    
        for (int l = 1; l < n + 1; ++l)
        {
            tmp = new List<Tuple<double, int>>();
            best.Add(tmp);
            for (int j = 0; j < words.Length + 1; ++j)
            {
                var min = new Tuple<double, int>(best[l - 1][0].Item1 + cost(0, j), 0);
                for (int k = 0; k < j + 1; ++k)
                {
                    var loc = best[l - 1][k].Item1 + cost(k, j);
                    if (loc < min.Item1 || (loc == min.Item1 && k < min.Item2))
                        min = new Tuple<double, int>(loc, k);
                }
                tmp.Add(min);
            }
        }
    
        var lines = new List<string>();
        var b = words.Length;
    
        for (int l = n; l > 0; --l)
        {
            var a = best[l][b].Item2;
            lines.Add(string.Join(" ", words, a, b - a));
            b = a;
        }
    
        lines.Reverse();
        return lines;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know a good algorithm for perform clustering on both discrete and continuous
Does anyone know good way to profile a sorting algorithm in java(sequential and fork
Good morning, Does anyone know about efficient algorithms for partial string matching? For example,
Does anyone know a good Java lib that will hook into SVN so I
Does anyone know any good tool that I can use to perform stress tests
Does anyone know a good resource to concisely explain the different types of lists
Does anyone know any good tutorial about using NAnt for native code build process
Does anyone know any good and free .NET Windows Controls Library
Does anyone know a good online resource for example of R code? The programs
Does anyone know a good C++ class to read (and possibly also write) INI

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.