Browse Source

implemented readInput

remotes/origin/userinput
TheUltimateOptimist 11 months ago
parent
commit
eef850ed11
  1. 25
      src/userinput.c

25
src/userinput.c

@ -2,6 +2,7 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
void trimLeft(char *input) {
size_t length = strlen(input);
@ -22,3 +23,27 @@ void trimRight(char *input) {
index--;
}
}
char *readInput() {
size_t bufferSize = 100;
char *buffer = malloc(bufferSize*sizeof(char));
char c;
int index = 0;
do {
c = fgetc(stdin);
if (!(bufferSize > index)) {
char *newBuffer = malloc(2*bufferSize*sizeof(char));
for (int i = 0; i < bufferSize; i++) {
newBuffer[i] = buffer[i];
}
bufferSize *= 2;
}
buffer[index] = c;
index++;
}
while(c != '\n' && c != EOF);
buffer[index - 1] = '\0';
trimLeft(buffer);
trimRight(buffer);
return buffer;
}
Loading…
Cancel
Save