Thread: folding lines...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    folding lines...

    Write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.

    If i for example enter a tab, Do i have to count it as a number of x blank characters, depending on the tab stop? How exactly should i handle tabs?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tool
    If i for example enter a tab, Do i have to count it as a number of x blank characters, depending on the tab stop? How exactly should i handle tabs?
    I think you should handle it in that way as it makes sense to me.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    edit: fixed
    Last edited by Tool; 11-22-2009 at 02:58 PM.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    What about the most 'intelligent' way to handle lines with no blanks/tabs before the nth column? What would be the best thing to do?
    I was thinking about moving the whole array into right by one and inserting a new \n at that spot?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Be practical - how many words are there in the English lexicon that span a line about 1000 characters long

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Tabs can be fun when you get into coding up your own editor.

    For your assignment, I'd suggest making a tab equal to 8 blank spaces. That's a common default value for text editors, and yet keeps it simple.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Quote Originally Posted by itCbitC View Post
    Be practical - how many words are there in the English lexicon that span a line about 1000 characters long
    Yeah, but i do want to handle this possibility aswell.

    Im working with a LINE_WIDTH constant, so lets say i want it to have a max line width 10, and some words are more then 10 chars long.

    What would be the best option? To give a warning about it? I know its not that important... but still.
    Last edited by Tool; 11-28-2009 at 08:47 AM.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    The way my program handles lines with no blank before the nth column is it reports an error and doesnt fold the line.

    I seperated the program into 2 functions for simplicity:
    --1. Getline - gets a line, replaces tabs with the proper number of blanks and takes care of overflow (MAX_SIZE)
    --2. Fold_line -- folds the line based on LINE_WIDTH constant. if no blanks before the nth column returns 0 and reports an error.

    If you guys think i did something wrong tell me. I think i did everything this exercise asks for, but if you think i can improve it somehow or take care of the long lines in a better way, id appreciate if you told me .

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_SIZE 100
    #define LINE_WIDTH 10
    #define TAB_STOP 8
    
    int Getline(char line[]) /* replaces tabs with the proper amounts of blanks & takes care of overflow */
    {
         int c, counter, x, y, tmp;
         counter = x = y = tmp = 0;
         while((c=getchar())!='\n')
         {
            if(c == EOF || x == MAX_SIZE - 2) return 0;
            
            if(counter != TAB_STOP + 1) counter++; 
            else counter = 0;
            
            if(c=='\t')
            {
               tmp = TAB_STOP+1 - counter;
               for(y=0; y<tmp; y++) line[x++]=' ';
               counter = 0;
            }
            else line[x++]=c;
         }
         
         line[x]='\n'; x++;
         line[x]='\0';
         
         return x;
    }
    
    int Fold_line(char line[], int x) /* folds lines, reports an error if there are no blanks before the nth column */
    {
         int pom=x;
         int counter, blank_location, word_len;
         counter = blank_location = word_len =0;
         
         for(x=0; x<pom; x++)
         {
            if(line[x]==' ') { blank_location=x; word_len = 0; }
            else word_len++;
            
            if(counter == LINE_WIDTH+1 && word_len < LINE_WIDTH )
            {
               if(line[x]==' ') { line[x]='\n'; word_len=0; counter=0; }
               else { line[blank_location]='\n'; counter = word_len; }
            }
            else if(counter >= LINE_WIDTH+1 && word_len >= LINE_WIDTH + 1) return 0;
    
            counter++;
         }
         return 1;
    }
    
    int main(int argc, char *argv[])
    {
        
        int x=0;
        int tmp=0;
        char line[MAX_SIZE];
        
        do{
        if((x = Getline(line))==0) return 0;
      
        if((tmp = Fold_line(line, x))==0) printf("Sequence of characters found without a blank before the %d. column.\n", LINE_WIDTH+1);
        else printf("%s", line);
        
        }while(x!=0);
    
        printf("Press any key to continue\n\n");
        getchar();
        return 0;
    }
    Last edited by Tool; 11-28-2009 at 12:12 PM.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Tool View Post
    Yeah, but i do want to handle this possibility aswell.

    Im working with a LINE_WIDTH constant, so lets say i want it to have a max line width 10, and some words are more then 10 chars long.

    What would be the best option? To give a warning about it? I know its not that important... but still.
    Perhaps then see hyphenation which is used for wrapping long lines by breaking words into fragments.

  10. #10
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    What about this input:

    Code:
    #define LINE_WIDTH 10
    ...
    char line[40]=
    "1234567890     test"
    ...
    Fold_line(line);
    
    
    Should i insert a \n right before test:
    "1234567890    \ntest"
    
    Or after the LINE_WIDTH spot:
    "1234567890\n    test"
    Basicaly, should i "discard" all the blanks/tabs before the next character after max LINE_WIDTH?
    Last edited by Tool; 11-30-2009 at 10:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read a well defined number of lines only
    By cfdprogrammer in forum C Programming
    Replies: 10
    Last Post: 10-01-2009, 08:55 AM
  2. Folding Input Lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-22-2008, 05:11 PM
  3. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  4. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  5. Reading lines from a file
    By blackswan in forum C Programming
    Replies: 9
    Last Post: 04-26-2005, 04:29 PM