Changing 3 spaces to tabs

This is a discussion on Changing 3 spaces to tabs within the C Programming forums, part of the General Programming Boards category; So my program is suppose to change 3 run-on spaces from an input into a single tab in the output. ...

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

    Changing 3 spaces to tabs

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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You always add one to i, but you don't always write something to to. You should only add one to i if a character was actually written out.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    thanks again for helping with another tab problem, tabstop hehe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 12:45 PM
  2. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  3. Replies: 5
    Last Post: 06-30-2003, 01:52 PM
  4. tabs to spaces with dev c++ v.4
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 02:07 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

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