SHARE
TWEET

Untitled

a guest Jan 21st, 2020 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stringhe.h"
  3.  
  4. char *spacefill(const char *str, size_t n)
  5. {
  6.     if(str == NULL)
  7.         return NULL;
  8.  
  9.  
  10.     int len = strlen(str);
  11.     if (len >= n) {
  12.         char *s = malloc((len+1) * sizeof(char));
  13.         s[len] = 0;
  14.         s = strcpy(s, str);
  15.         return s;
  16.     }
  17.  
  18.     size_t size = n - len;
  19.     char *s = malloc(size*sizeof(char));
  20.    
  21.     memset(s, 32, size);
  22.  
  23.     s = realloc(s, (size + 1) * sizeof(char));
  24.     s[size] = 0;
  25.     size++;
  26.  
  27.     char *res = malloc((size-1 + strlen(str)) * sizeof(char));
  28.     res = strcat(s, str);
  29.     free(s);
  30.  
  31.     return res;
  32.  
  33. }
  34.  
  35. int main(void) {
  36.  
  37.     char str[] = "ciao";
  38.     char *res = spacefill(str, 10);
  39.     free(res);
  40.  
  41.     return 0;
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top