Folding Input Lines

This is a discussion on Folding Input Lines within the C Programming forums, part of the General Programming Boards category; Hello all, so my program is suppose to take in an input and every 5 characters make a new line ...

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

    Folding Input Lines

    Hello all, so my program is suppose to take in an input and every 5 characters make a new line for the next maximum of 5 characters. It seems like its copying correctly but everytime it copies an input that is able to be split up into 5 characters perfectly, it adds an additional empty line. Other odd lines of input work fine though. I think it has something to do with how I am setting to[i] to be a new line. However I am unsure of how to fix it.

    Code:
    #include <stdio.h>
    #define MAXLINE 1000
    #define FOLD 5
    
    int getline(char line[]);
    void copy(char to[], char from[]);
    
    //folds any given length input into 
    //lines of 5 characters
    main(){
    	int len; //current line length
        char unfolded[MAXLINE]; //current input line
    	char folded[MAXLINE]; //folded line saved here
    	
    	len = 0;
    
    	while ((len = getline(unfolded)) > 0){
    			copy(folded, unfolded);
    			printf("%s", folded);
    		}
    		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
    //the folding copy
    void copy(char to[], char from[]){
    	int i, i2, foldnum;
    
    	i = 0;
    	i2 = 0;
    	foldnum = 0;
    
    	while (from[i2] != '\0'){
    
    		//while foldnum is not equal to FOLD
    		//copy normally
    		//increment foldnum by 1
    		if(foldnum != FOLD){
    			to[i] = from[i2];
    			i++;
    			i2++;	
    			++foldnum;
    			}
    
    		//when foldnum is equal to FOLD
    		//make a new line
    		//reset foldnum
    		//increase i by 1
    		else if (foldnum == FOLD){
    			to[i] = '\n';
    			foldnum = 0;
    			++i;
    			}
    		}
    
    	to[i] = '\0';
    	}
    Last edited by dnguyen1022; 12-22-2008 at 02:18 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    The code doesn't take into account the case when there are fewer than 5 characters between the newline last inserted and the end of the current line.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    IIRC the original question asked in this post was a lot different, it had something to do with proper word wrapping, from the one posed below. Perhaps you fixed that issue and a new one came up. Anyways all line lengths that are multiples of 5 have an extra empty line in the output because the first newline is added by the code to make a line 5 characters long, the second is added when copying the next character from the source array from[] into the target array to[]. See the changes shown in red below.
    Quote Originally Posted by dnguyen1022 View Post
    Hello all, so my program is suppose to take in an input and every 5 characters make a new line for the next maximum of 5 characters. It seems like its copying correctly but everytime it copies an input that is able to be split up into 5 characters perfectly, it adds an additional empty line. Other odd lines of input work fine though. I think it has something to do with how I am setting to[i] to be a new line. However I am unsure of how to fix it.

    Code:
    #include <stdio.h>
    #define MAXLINE 1000
    #define FOLD 5
    
    int getline(char line[]);
    void copy(char to[], char from[]);
    
    //folds any given length input into 
    //lines of 5 characters
    main(){
    	int len; //current line length
        char unfolded[MAXLINE]; //current input line
    	char folded[MAXLINE]; //folded line saved here
    	
    	len = 0;
    
    	while ((len = getline(unfolded)) > 0){
    			copy(folded, unfolded);
    			printf("%s", folded);
    		}
    		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
    //the folding copy
    void copy(char to[], char from[]){
    	int i, i2, foldnum;
    
    	i = 0;
    	i2 = 0;
    	foldnum = 0;
    
    	while (from[i2] != '\0'){
    
    		//while foldnum is not equal to FOLD
    		//copy normally
    		//increment foldnum by 1
    		if(foldnum != FOLD){
    			to[i] = from[i2];
    			i++;
    			i2++;	
    			++foldnum;
    			}
    
    		//when foldnum is equal to FOLD
    		//make a new line
    		//reset foldnum
    		//increase i by 1
    		else if (foldnum == FOLD){
                        if (from[i2] != '\n') {
                             to[i] = '\n';
                             ++i;
                        }
                        foldnum = 0;
                    }
          	     }
    	to[i] = '\0';
    	}

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Take this bit out and see what happens.
    I am not sure what it is for it seems to add and extra linefeed.
    Code:
    if(c == '\n'){
    		s[i] = c;
    		++i;
    		}
    Last edited by esbo; 12-22-2008 at 05:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 12:43 PM
  4. Replies: 3
    Last Post: 06-25-2003, 05:29 PM
  5. input files in C
    By LWisne in forum C Programming
    Replies: 4
    Last Post: 09-30-2002, 07:24 AM

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