Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. void stdin_function () {
  2.    
  3.     char *ch;
  4.     int size = 10;
  5.     int new_line = 0; //number of new lines
  6.     ch =  malloc (size * sizeof(char));
  7.     int i;
  8.     for (i = 0;scanf("%c",&ch[i]) != EOF;i++) {
  9.         if(i >= size) {
  10.             size += 2;
  11.             ch = realloc (ch,size);
  12.         }
  13.     }
  14.     int reverse = i - 1;        //counts in the reversed loop (i-1) is because we want the index
  15.     char arr[size];         //static arr with capacity size
  16.     int br = -1;            //the counter which will be used to go through arr
  17.     for(;reverse != -1; reverse --) { // -1 we want to read the 0 element too
  18.         if(ch[reverse] == '\n') {
  19.             new_line ++;
  20.             if (new_line == 11) break; //breaks if we have read 10 \n
  21.         }
  22.         arr[++br] = ch[reverse];    //++br to become 0 in the beginning and to have br as the index not the size
  23.     }
  24.  
  25.     for (; br > -1 ;br -- ) {
  26.         write(STDOUT_FILENO,&arr[br],1);
  27.     }
  28.     free(ch);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement