L1-032. Left-pad, l1-032left-pad
According to a message on Sina Weibo, a developer is not satisfied with the NPM (Node Package Manager) practice and has withdrawn his open source code, including a module called left-pad, this module breaks down the React/Babel in javascript. What kind of module is this? It is to fill something in front of the string to a certain length. For example, if "*" is used to fill the string "GPLT" with a length of 10, the result of calling left-pad should be "****** GPLT ". The Node community once urgently released an alternative to left-pad, which was severely frustrated. The following describes how to implement this module.
Input Format:
The input is a positive integer N (<= 104) and a character in the first line, which is the length of the filling result string and the character used for filling, separated by a space in the middle. The second line shows the original non-empty string and ends with a carriage return.
Output Format:
Output The result string in a row.
Input Example 1:
15 _I love GPLT
Output Example 1:
____I love GPLT
Input Example 2:
4 *this is a sample for cut
Output Example 2:
cut
Time Limit: 400 ms memory limit: 65536 kB code length limit: 8000 B Judgment program Standard author Chen Yue Note: 1. the character string can be read here, so gets () is used to read the string, and scanf () is used to read the string. If it encounters a space, the reading ends. 2. Here, the maximum value of N is 10000, but the input string may be larger than 10000. So how big is the definition? The question memory is limited to 65536 kB, with several MB of memory, so it does not matter if the input string is larger.
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ int n; char x; int i, j = 0; char arr[40000] = {' '}; char arr1[40000] = {' '}; scanf("%d ", &n); scanf(" %c ", &x); if(n <= 0 || n > 10000) exit(0); gets(arr); if(strlen(arr) < n) { for(i = 0; i < n - strlen(arr); i++) { arr1[i] = x; } for(i = n - strlen(arr); i < n; i++) { arr1[i] = arr[j]; j++; } printf("%s\n", arr1); } else { j = 0; for(i = strlen(arr) - n; i < strlen(arr); i++) { arr1[j] = arr[i]; j++; } printf("%s\n", arr1); } return 0;}