Originally Posted by
itCbitC
Can you provide some explanation of your code through inline comments etc. as it is hard to understand what is goin' on.
With getline() you're restricted to line lengths of MAXLINE; a simple getchar() that terminates on EOF works much better.
Forgive me, but you've got to have some bounds on the string. I suppose I could read it in and print it a character at a time, but since I'm reading it into a string, whether I do it by line or by getchar() terminating on EOF, I've got to keep a limit on it in some way, don't I?
I try to follow the Linus Torvalds coding rules, but my comments are still too sparse. Here's a play-by-play of what I'm going for:
Code:
#include<stdio.h>
#include<string.h>
#define MAXLINE 10000
#define TABSTOP 8
int getline(char s[], int lim);
int tabreplace(char s[], int tabspot);
int main(void)
{
int i,j,k;
char string[MAXLINE];
while(getline(string, MAXLINE) > 0) {
for(i=0; i<=strlen(string); i+=TABSTOP) {
i -= tabreplace(string,i);
}
printf("%s",string);
}
return 0;
}
This, of course, is pretty simple. While there's input from getline, execute the for loop. The loop starts at zero and increments by TABSTOP every cycle, until the index is greater than the length of the string it's processing. For each multiple of TABSTOP, call the function tabreplace(); tabreplace() returns the number of spaces replaced by the tab, so while we're at it we set the loop index back that number of spaces. When we're done, we print the string, then wait for a new line.
Code:
int tabreplace(char s[], int tabspot)
{
int i;
int skipback;
if (s[tabspot] == '_') {
Clear enough; if the character at TABSTOP, or a multiple thereof, is '_', execute the following code.
Code:
for (i=tabspot; s[i]=='_' && i>(tabspot-TABSTOP); --i);
Determines the numbers of spaces prior to tabspot that need to be replaced. Only trailing spaces need to be replaced, not leading ones, so it stops when it hits either a non-'_' or the prior tab. This means that the index is one behind where I need to insert the tab, so:
I insert the tab.
Code:
skipback = tabspot - (i+2);
for (i=i+2; i<strlen(s); ++i)
s[i] = s[i+skipback];
printf("returning %d\n",skipback);
Then I set skipback equal to the number of spaces I replaced with the '\t'. It looks worse than it is. It's the tabspot, minus the number of spaces to the first non-space character, plus that first non-space character and the '\t' itself.
I then run a loop to move the entire string back by skipback, one character at a time, to ensure that not only is a tab inserted by that the spaces are actually removed.
The printf was a debugging statement to make sure that I was returning the correct value.
Code:
return skipback+1;
}
Then I return the number of spaces I replaced (plus one for the '\t' I inserted).}
I'm using the underscore for spaces so that I can visually verify what's been replaced and what hasn't.
I've skipped getline(), as its function seems obvious. It gets a line, returns the length of said line.
So do you all think I'm at least on the right track?