Lets say I want to write a very simple C program wich a is string passed with a pipe at my program and translate every letter to ASCII and store it in an array. I want to use less library as possible and not use malloc
For example in a Linux terminal : echo Translate this!| ./myprogram
So I tough that I should count the letters and initialise an array the size of the string.
#include <stdio.h>
#include <ctype.h>
int main()
{ int c;
int count = 0;
while ((c = getchar()) != EOF)
count = count + 1;
int array[count];
return 0;
}
It seems to me that getchar "clears" my input and my string is not reusable. The problem is that I cannot manage to reuse getchar() to go through my string again. Lets say the input will be always variable. Sometimes it could be 10 letters, sometimes 1 million letters. How can I manage to store my input in an array without malloc ?
I tough doing somekind of temporary array and translate n letters at a time ? Is it a more suitable option ?
Thanks !