Advertisement
informaticage

get lines

Jul 31st, 2021
1,426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. size_t getlines(char* s, size_t lim) {
  5.   size_t n = 0;
  6.   char c;
  7.  
  8.   while(n < lim && (c = getchar()) != EOF && c != '\n') {
  9.     *s = c;
  10.     n++;
  11.     s++;
  12.   }
  13.  
  14.   if (c == '\n') *s = '\n';
  15.   return n;
  16. }
  17.  
  18. int main() {
  19.   char* str = malloc(sizeof(char) * 15);
  20.   getlines(str, 15);
  21.   printf("%s", str);
  22.   return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement