Happy New Year Wish String
Happy New Puzzle Program
- String[] happyNewYear(String wish) {
- String[] arr = wish.split(" ");
- String[] finalStr = new String[arr.length+2];
- String begin = "";
- int bigcount = 0;
- int i = 1;
- for ( String ss : arr) {
- if (bigcount < ss.length()) {
- bigcount = ss.length();
- }
- finalStr[i] = ss ;
- i++;
- }
- for (int j =0; j< bigcount+4; j++) {
- begin += "*" ;
- }
- String format ="* ";
- for (int n = 2; n < bigcount+3; n++) {
- format += " ";
- }
- format +="*";
- for(int k = 1; k < finalStr.length-1; k++) {
- String word = "* " + finalStr[k];
- int wordCount = word.length();
- StringBuffer buf = new StringBuffer(format);
- int start = 0;
- int end = wordCount;
- buf.replace(start, end, word);
- System.out.println(buf);
- finalStr[k] = buf + "";
- }
- finalStr[0] = new String(begin);
- finalStr[finalStr.length-1] = new String(begin);
- return finalStr;
- }
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
Post a Comment