Happy New Year Wish String

Happy New Puzzle Program



  1. String[] happyNewYear(String wish) {
  2. String[] arr = wish.split(" ");
  3. String[] finalStr = new String[arr.length+2];
  4. String begin = "";
  5. int bigcount = 0;
  6. int i = 1;
  7. for ( String ss : arr) {
  8. if (bigcount < ss.length()) {
  9. bigcount = ss.length();
  10. }
  11. finalStr[i] = ss ;
  12. i++;
  13. }
  14. for (int j =0; j< bigcount+4; j++) {
  15. begin += "*" ;
  16. }
  17. String format ="* ";

  18. for (int n = 2; n < bigcount+3; n++) {
  19. format += " ";
  20. }
  21. format +="*";

  22. for(int k = 1; k < finalStr.length-1; k++) {
  23. String word = "* " + finalStr[k];
  24. int wordCount = word.length();
  25. StringBuffer buf = new StringBuffer(format);
  26. int start = 0;
  27. int end = wordCount;
  28. buf.replace(start, end, word);
  29. System.out.println(buf);
  30. finalStr[k] = buf + "";
  31. }
  32. finalStr[0] = new String(begin);
  33. finalStr[finalStr.length-1] = new String(begin);
  34. return finalStr;
  35. }





Input(s)
wish: "Happy New Year and CodeFight On in 2016!"

Output
["*************",
"* Happy *",
"* New *",
"* Year *",
"* and *",
"* CodeFight *",
"* On *",
"* in *",
"* 2016! *",
"*************"]

Expected Output
["*************",
"* Happy *",
"* New *",
"* Year *",
"* and *",
"* CodeFight *",
"* On *",
"* in *",
"* 2016! *",
"*************"]

Console Output
* Happy *
* New *
* Year *
* and *
* CodeFight *
* On *
* in *
* 2016! *

Comments