Guest User

Untitled

a guest
Aug 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. char *getString()
  2. {
  3.      int nSize = 2;
  4.      char *str;
  5.      str = (char *) malloc (nSize * sizeof(*str));
  6.      str[1] = 0;
  7.      
  8.      int nIndex = 0;
  9.      do
  10.      {
  11.          str[nIndex] = getch();
  12.          printf("%c", str[nIndex]);
  13.          
  14.          nIndex++;
  15.            
  16.          if (nIndex == nSize)
  17.          {
  18.               char *strTmp;
  19.               nSize += 1;
  20.               strTmp = (char *) realloc (str, nSize * sizeof(*str));
  21.               str = strTmp;
  22.               str[nSize - 1] = 0;
  23.          }              
  24.       }while (str[nIndex - 1] != 13);
  25.    
  26.       char *strTmp;
  27.       strTmp = (char *) realloc (str, (nSize - 2) * sizeof(*str));
  28.       str = strTmp;
  29.       str[nSize - 2] = 0;
  30.      
  31.       return str;
  32. }
Add Comment
Please, Sign In to add comment