Easy

Given a positive integer number and a certain length, we need to modify the given number to have a specified length. We are allowed to do that either by cutting out leading digits (if the number needs to be shortened) or by adding 0s in front of the original number.

Example

  • For number = 1234 and width = 2, the output should be
    integerToStringOfFixedWidth(number, width) = "34";
  • For number = 1234 and width = 4, the output should be
    integerToStringOfFixedWidth(number, width) = "1234";
  • For number = 1234 and width = 5, the output should be
    integerToStringOfFixedWidth(number, width) = "01234".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer number

    A non-negative integer.

    Guaranteed constraints:
    0 ≤ number ≤ 10^9.

  • [input] integer width

    A positive integer representing the desired length.

    Guaranteed constraints:
    1 ≤ width ≤ 50.

  • [output] string

    • The modified version of number as described above.

[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

char *integerToStringOfFixedWidth(int number,int width)
{
	char *str=malloc(sizeof(char)*(width+1));

	for(int i=width-1;i>=0;i--,number/=10)
		str[i]=number%10+'0';

	return str;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Three Split  (0) 2020.05.24
<Codesignal> Ada Number  (0) 2020.05.23
<Codesignal> Timed Reading  (0) 2020.05.04
<Codesignal> Switch Lights  (0) 2020.05.04
<Codesignal> Minimal Number of Coins  (0) 2020.05.04

+ Recent posts