### Does this program have a pattern sequence?

In the below program, i and l diverge when the input string contains a tab. i is a counter to iterate the Buffer and l is a counter that keeps track of chars put to the output stream. For example, in the following use case: abc\tdef\t\tghi\t\t\tjkl\tmno\n , where \t is tab, i and l will both increment at the same rate until the outer for loop in main hits the tab, then l increases faster than i, since more spaces are put in output stream for each tab in the Buffer.

That leads to my question: **I don't see a sequence pattern here**. It seems like if tabs are put in different places or there are multiple tabs one after the other, you are going to get different results. For example, in the above use case, when the first tab is hit, two spaces are printed (e.g. l is equal to 3 and 3 / 5 (TabSize) has a remainder of 3, and subtracting 5 - 3 returns 2). Another time, the CalculateNumberOfSpaces function returns 5. I tried different use cases, and they are all producing different results depending on where tab is and if multiple tabs are adjacent.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119`` `````` #include #include #include #define MAX_BUFFER 1024 #define SPACE ' ' #define TAB '\t' int CalculateNumberOfSpaces(int Offset, int TabSize) { return TabSize - (Offset % TabSize); } /* K&R's getline() function from p29 */ int getline(char s[], int lim) { int c, i; for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if(c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } int main(void) { char Buffer[MAX_BUFFER]; int TabSize = 5; /* A good test value */ int i, j, k, l; while(getline(Buffer, MAX_BUFFER) > 0) { for(i = 0, l = 0; Buffer[i] != '\0'; i++) { if(Buffer[i] == TAB) { j = CalculateNumberOfSpaces(l, TabSize); for(k = 0; k < j; k++) { putchar(SPACE); l++; } } else { putchar(Buffer[i]); l++; } } } return 0; }``````

Is this using some kind of pattern sequence that I don't see? And when I say pattern sequence I mean something like you do in algebra:

 ``12`` `````` f(n) = 3(n - 1) + 2 // 2,5,8,11,14,17,20,23,... ``````

This program is taken from exercise guide in "C Programming language" book:
http://users.powernet.co.uk/eton/kandr2/krx120.html
¿why the double spacing?

I don't realize what is your question
The purpose of the program is to display tabs "properly". The idea is that there is a marker every TABSIZE characters, when you press TAB, the text would advance to the next marker
By instance (TABSIZE=8)
 ```a 1 alpha ab 22 beta abc 333 gamma abcd 4444 delta```
Topic archived. No new replies allowed.