Java Program to Implement Word Wrap Problem

This Java program Implements Word Wrap Problem.A Given a sequence of words, and a limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly. Assume that the length of each word is smaller than the line width.

Here is the source code of the Java Program to Implement Word Wrap Problem.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. public class WordWrapProblem
  2. {
  3.     private static final int INFINITY = Integer.MAX_VALUE;
  4.  
  5.     void solveWordWrap(int l[], int n, int M)
  6.     {
  7.         int extras[][] = new int[n + 1][n + 1];
  8.         int lineCost[][] = new int[n + 1][n + 1];
  9.         int cost[] = new int[n + 1];
  10.         int printSol[] = new int[n + 1];
  11.         int i, j;
  12.  
  13.         for (i = 1; i <= n; i++)
  14.         {
  15.             extras[i][i] = M - l[i - 1];
  16.             for (j = i + 1; j <= n; j++)
  17.             {
  18.                 extras[i][j] = extras[i][j - 1] - l[j - 1] - 1;
  19.             }
  20.         }
  21.  
  22.         for (i = 1; i <= n; i++)
  23.         {
  24.             for (j = i; j <= n; j++)
  25.             {
  26.                 if (extras[i][j] < 0)
  27.                 {
  28.                     lineCost[i][j] = INFINITY;
  29.                 } else if (j == n && extras[i][j] >= 0)
  30.                 {
  31.                     lineCost[i][j] = 0;
  32.                 } else
  33.                     lineCost[i][j] = extras[i][j] * extras[i][j];
  34.             }
  35.         }
  36.         cost[0] = 0;
  37.         for (j = 1; j <= n; j++)
  38.         {
  39.             cost[j] = INFINITY;
  40.             for (i = 1; i <= j; i++)
  41.             {
  42.                 if (cost[i - 1] != INFINITY && lineCost[i][j] != INFINITY
  43.                     && (cost[i - 1] + lineCost[i][j] < cost[j]))
  44.                 {
  45.                     cost[j] = cost[i - 1] + lineCost[i][j];
  46.                     printSol[j] = i;
  47.                 }
  48.             }
  49.         }
  50.         printSolution(printSol, n);
  51.     }
  52.  
  53.     private int printSolution(int p[], int n)
  54.     {
  55.         int k;
  56.         if (p[n] == 1)
  57.         {
  58.             k = 1;
  59.         } else
  60.         {
  61.             k = printSolution(p, p[n] - 1) + 1;
  62.         }
  63.         System.out.println("Line number " + k + " From word no " + p[n] + " to " + n);
  64.         return k;
  65.     }
  66.  
  67.     public static void main(String...arg)
  68.     {
  69.         int l[]  = {3,2,2,5};
  70.         int n = 4;
  71.         int M = 6;
  72.         WordWrapProblem wordWrapProblem = new WordWrapProblem();
  73.         wordWrapProblem.solveWordWrap(l, n, M);
  74.     }	
  75. }

$ javac WordWrapProblem.java
$ java WordWrapProblem
Line number 1 From word no 1 to 1
Line number 2 From word no 2 to 3
Line number 3 From word no 4 to 4

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.