So my program is suppose to change 3 run-on spaces from an input into a single tab in the output. If it comes across 2 spaces, it will replicate the 2 spaces. If it comes across 4 spaces, it will change the 3 spaces into a tab and then output the single leftover space. As of right now my program does funny stuff when it runs into a space...I am unsure where I am doing it wrong. Maybe you guys can help me find out my place(s) of error. 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 3 spaces replaced by tabs //spaces of 2 or 1 between other letters or number //will remain the same main(){ int len; //current line length char spaced[MAXLINE]; //current input line char tabbed[MAXLINE]; //detabbed line saved here len = 0; while ((len = getline(spaced)) > 0){ copy(tabbed, spaced); printf("%s", tabbed); } 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 continuous tabs with 3 spaces void copy(char to[], char from[]){ int i, i2, tmp, tmp2, x; i = 0; i2 = 0; tmp = 0; //while from[i2] is not equal to end of line while (from[i2] != '\0'){ //if from[i2] is equal to a blank //add 1 to tmp if (from[i2] == ' '){ ++tmp; //if tmp is ever equal to 3 //set to[i] to become a tab //reset t if (tmp == 3){ to[i] = '\t'; tmp = 0; } } //else if from[i2] is not a blank else if (from[i2] != ' '){ //if tmp is equal to 2 //set to[i] and to[i+1] //to be blanks if (tmp == 2){ tmp = 0; for(x = 0; x != 2; ++x){ to[i] = ' '; ++i; } to[i] = from[i2]; } //if tmp is equal to 1 //set to[i] to be blanks else if (tmp == 1){ tmp = 0; to[i] = ' '; ++i; to[i] = from[i2]; } //otherwise copy from[i2] //to to[i] as normal else to[i] = from[i2]; } ++i; ++i2; } to[i] = '\0'; }



LinkBack URL
About LinkBacks


