Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "stringhe.h"
- char *spacefill(const char *str, size_t n)
- {
- if(str == NULL)
- return NULL;
- int len = strlen(str);
- if (len >= n) {
- char *s = malloc((len+1) * sizeof(char));
- s[len] = 0;
- s = strcpy(s, str);
- return s;
- }
- size_t size = n - len;
- char *s = malloc(size*sizeof(char));
- memset(s, 32, size);
- s = realloc(s, (size + 1) * sizeof(char));
- s[size] = 0;
- size++;
- char *res = malloc((size-1 + strlen(str)) * sizeof(char));
- res = strcat(s, str);
- free(s);
- return res;
- }
- int main(void) {
- char str[] = "ciao";
- char *res = spacefill(str, 10);
- free(res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement