Thread: A Debtab Program Help

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    A Debtab Program Help

    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;
    		}
    	}

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I would iterate through the original string, keeping a "state" variable, where the states can be, for example,

    1) normal
    2) skip tabs

    then -
    Code:
    int i;
    int len = strlen(orig);
    int state = 1;
    for (i = 0; i < len; ++i) {
         switch (state) {
              case 1:
                    //copy orig[i] to to[i] if it's not a tab
                    //otherwise write 3 spaces, and set state to 2
              case 2:
                    //if orig[i] is a tab, ignore, otherwise copy to to[i] and set state to 1
         }
    }

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Without reading very carefully, this looks suspicious.
    Code:
    while ((to[i] = from[i2]) != '\0'){
    		if (from[i] == '\t'){

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You WHILE loop does an assignment and a test for end of string. I would simplify it to just check for the end of the "from" string.

    Inside your WHILE loop,
    IF the character is a tab, write 3 blanks to "to" and update i by TAB-1
    ELSE copy from into to

    THEN, increment both i and i2.

    Done.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM