A Debtab Program Help

This is a discussion on A Debtab Program Help within the C Programming forums, part of the General Programming Boards category; So my program is suppose to try to find to tabs from the input and output it with the tabs ...

  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,183
    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,185
    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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

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, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21