Hello all! This is my first thread! I actually signed up to post, because i saw that several people here on the forum have asked about this exercise in the past. Basically, the task is to replace tabs with spaces, ensuring that the number of spaces is appropriate to get you to the next tab stop (i.e. if you were only 4 spaces away from a tab stop, don't replace the tab with 8 spaces).

Anyway, i'm not posting because i can't figure it out. I'm posting because i did figure it out, and i'm not sure if my code is good enough. I think all of the solutions i've seen have included character arrays, and many have included multiple functions. I realize the text says "these exercises suggest programs of somewhat greater complexity than the ones earlie in this chapter," but it seemed like a very straightfortward task.

Have i oversimplified or something?

Code:
#include <stdio.h>

#define TAB_STOP 8

int main()
 {
  int c, i;

  c=0;

  for (i=TAB_STOP; (c=getchar()) != EOF; --i)
   {
    if (i<=0)
      i=TAB_STOP;

    if (c=='\n')
      i=TAB_STOP+1;

    if (c=='\t')
      while (i>0)
       {
        printf(" ");
        --i;
       }
    else
      putchar(c);
   }

  return 0;
 }