logoalt Hacker News

dlcarrieryesterday at 4:12 PM2 repliesview on HN

It's a little awkward, because you'd need to parse the strings in reverse, but if all you need to do is sum, you can do it one digit at a time, while at any given moment only handling only one character from each input string, a carry byte, and one output character.


Replies

ronsoryesterday at 8:21 PM

You don't need to parse the strings in reverse. That's for printing integers, not parsing. Roughly:

    int stdin_atoi() {
      int i = 0;
      while (1) {
        int c = getchar();
        if (c >= '0' && c <= '9') {
          i = i * 10 + (c - '0');
        } else { break; }
      }
      return i;
    }
show 2 replies
pbalauyesterday at 7:25 PM

How do you know where the first string ends and the second starts? Did you miss the "stdin" part?

This is not

    ./program first_number second_number
show 1 reply