SHARE
TWEET

getstr

a guest Feb 14th, 2020 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. char *getstr() {
  2.     char *ptr = (char *)malloc(1);
  3.     char buf[81];
  4.     int n, len = 0;
  5.     *ptr = '\0';
  6.     do {
  7.         n = scanf_s("%80[^\n]", buf, 81);
  8.         if (n < 0) {
  9.             free(ptr);
  10.             ptr = NULL;
  11.             continue;
  12.         }
  13.         if (n == 0)
  14.             scanf_s("%*c");
  15.         else {
  16.             len += strlen(buf);
  17.             ptr = (char *)realloc(ptr, len + 1);
  18.             int k = strcspn(buf, "\0"); // длина слова
  19.             int l = strlen(ptr) + k + 1;
  20.             strncat_s(ptr, l, buf, k);
  21.         }
  22.     } while (n > 0);
  23.     return ptr;
  24. }
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