'Given a rectangular matrix of characters, add a border of asterisks(*) to it

Example For

picture = ["abc",
           "ded"]

the output should be

addBorder(picture) = ["*****",
                      "*abc*",
                      "*ded*",
                      "*****"]

I tried to another array and append the '*' ith elements

for (int i=0; i<arrlen+2; i++)
    if(i==0 || i==arrlen+1){
        for(int j = 0; j<len+2; j++)
            pictures[i] = pictures[i].append("*")

and

else {
        for(int j = 0; j<len+2; j++)
            if(j==0 || i==len+1)
                pictures[i] = pictures[i].append("*");

But it says "cannot find symbol" at the .append()



Solution 1:[1]

You can simplify this without using .append. Use a for loop to print a row of asterisks, which should be the length of the string plus 2(1 extra for each side). To get the length of the string use .length()Then print an asterisk before and after the string using

System.out.println("*" + string + "*");

and print the same row of asterisks as in the first part.

The same goes for printing an array of strings, just use a for loop to print each value with System.out.println("*" + string[i] + "*"); then follow it with the same row of asterisks used for the top border.


Edit: After reading your question again closely I think you wanted the result stored in a new array. I started writing an example code and couldn't help but write the whole thing:

public static void main(String[] args) {
    String text [] = {"abc", "def", "ghi", "jkl"};
    String framedString [] = {"","","","","","",""}; //java arrays are fixed in size, 
                                                     //so I had to define 6 blank values
    String asterisks = "**";
    int lineCounter = 0;
    
    for (int i = 0; i < text[1].length(); i++) { //creates string of asterisks
        asterisks = asterisks + "*";
    }
    
    framedString[lineCounter] = asterisks; //first value becomes asterisk string
    
    for (int j = 0; j < text.length; j++) { //adds an asterisk before and after each string of "text" and stores it in "framedString"
        lineCounter = j+1;
        framedString[lineCounter] = "*" + text[j] + "*";
    }
    
    framedString[lineCounter+1] = asterisks; // last value becomes asterisk string as well
    
    for (int k = 0; k < framedString.length; k++) { //prints the result
        System.out.println(framedString[k]);
    }       
}

Solution 2:[2]

This solution can be broken down into 3 simple steps, 1st is to add the list of * at the top of the array, 2nd to add the * at the start and end of the element in the picture array by looping via the elements of the array and the last part is to add the list of * to the bottom of the array as the length of the last element of the array.

let picture =  ["abc", "ded"]; 

function addBorder(picture) {
 //adding the * on the top of the picture array as equal to the lenght of the top element of the array.
 let top = '*'.repeat(picture[0].length);
 picture.unshift(top);

 //looping through the array and concat the * at the end and the start of the array
 for(let i = 0; i < picture.length; i++){
     picture[i] = picture[i] + '*';
     picture[i] = '*' + picture[i];
 }

 //adding the * on the bottom of the picture array as equal to the lenght of the last element of the array.
 let bottom = '*'.repeat(picture[1].length);
 picture.push(bottom);
 
 return picture;
}

Solution 3:[3]

String[] addBorder(String[] picture) {
final int additionalCount = 2;
int elementLength = picture[0].length() + additionalCount;  // first element length which used to determine howmany * required.
int loopLength = picture.length + additionalCount; // extra two iteration for loop to add *** at the begining and end of array.

String [] output = new String[loopLength];

String border = "";
for( int i = 1; i <= elementLength; i++){
    border += "*";
}

for (int i = 0; i < loopLength; i++){
    if(i == 0){
        output[i] = border;
    } else if(i == loopLength-1){
         output[i] = border;
    } else {
        String s = "*"+picture[i-1]+"*";
        output[i] = s;
    }
}
return output;

}

Solution 4:[4]

function addBorder(picture) {
  let maxlength = 0
  for (let i=0;i<picture.length;i++){
    if(picture[i].length>maxlength){
      maxlength= picture[i].length
    }
  }
  let border = "*".repeat(maxlength+2);
  let resultArr =[border,border]
for (let i=0;i<picture.length;i++){
    resultArr.splice(i+1,0,"\*"+picture[i]+"\*")
} return resultArr
}

Solution 5:[5]

function solution(picture) {
const verticalBorder = '*'.repeat(picture[0].length);
picture.unshift(verticalBorder);
picture.push(verticalBorder);
return picture.map( (el) => '*' + el + '*' );

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Raja Shaiyan Azad Khan
Solution 3 Mahesh
Solution 4 eduestralop
Solution 5 jimibilibob