So my program is suppose to try to find to tabs from the input and output it with the tabs replaced it by a set number of spaces. In my case 1 tab = 3 spaces. It works with a single tab. However it seems if I do multiple tabs, it adds additional spaces. Can someone look over my code please? Thank you.
Code:
#include <stdio.h>
#define MAXLINE 1000
#define TAB 3
int getline(char line[]);
void copy(char to[], char from[]);
//prints out a line with tabs replaced by spaces
main(){
int len; //current line length
char tabbed[MAXLINE]; //current input line
char detabbed[MAXLINE]; //detabbed line saved here
len = 0;
while ((len = getline(tabbed)) > 0){
copy(detabbed, tabbed);
printf("%s", detabbed);
}
return 0;
}
//read a line into s, return length
int getline(char s[]){
int c, i;
for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
//copy from into to: assume to is big enough
//replaces all tabs with 3 spaces
void copy(char to[], char from[]){
int i, i2, tmp;
i = 0;
i2 = 0;
while ((to[i] = from[i2]) != '\0'){
if (from[i] == '\t'){
for(tmp = 0; tmp != TAB; ++tmp){
to[i] = ' ';
++i;
}
i = i - 1;
}
++i;
++i2;
}
}