How to write a getline function in C?

How to write a getline function in C?

If I understood you correct, the following should do it:

  1. read the whole line
  2. loop through the string as long as you get digits or the string ends
  3. for every digit, place its value in your array and increase the index by 1
while( ( c = getchar()) != EOF  &&  c != n &&  i < max ) {
    /* If desired, add check for value outside of 0-9 */
    array[ i++ ] = c - 0;
    ...
}

How to write a getline function in C?

char arr[] = 1234567;
int intarr[10];

int count = 0;
for (char* ptr = arr; *ptr; ptr++) {
  intarr[count] = *ptr - 0;
  count++;
}

Leave a Reply

Your email address will not be published. Required fields are marked *