Easy

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*",

                                               "*****"]

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.string picture

    A non-empty array of non-empty equal-length strings.

    Guaranteed constraints:
    1 ≤ picture.length ≤ 100,
    1 ≤ picture[i].length ≤ 100.

  • [output] array.string

    • The same matrix of characters, framed with a border of asterisks of width 1.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
arr_string addBorder(arr_string picture)
{
	arr_string returnPicture=alloc_arr_string(picture.size+2);
	for(int i=0;i<returnPicture.size;i++)
		returnPicture.arr[i]=(char *)malloc((strlen(picture.arr[0])+3)*sizeof(char));

	for(int i=0;i<strlen(picture.arr[0])+2;i++)
		returnPicture.arr[0][i]=returnPicture.arr[returnPicture.size-1][i]='*';

	for(int i=0;i<picture.size;i++)
		sprintf(returnPicture.arr[i+1],"*%s*", picture.arr[i]);

	return returnPicture;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> arrayChange  (0) 2020.04.05
<Codesignal> Are Similar?  (0) 2020.04.05
<Codesignal> alternatingSums  (0) 2020.04.05
<Codesignal> reverseInParentheses  (0) 2020.04.05
<Codesignal> Sort by Height  (0) 2020.04.05

+ Recent posts