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 <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
void trimLeft(char *input) { void trimLeft(char *input) {
size_t length = strlen(input); size_t length = strlen(input);
@ -21,4 +22,28 @@ void trimRight(char *input) {
input[index] = '\0'; input[index] = '\0';
index--; 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