/*************************************** ****
Write a program to "fold" long input rows into two or more shorter rows.
After the last non-space character before column N. Ensure that the program can intelligently process long input lines and
The specified column does not contain spaces or tabs.
**************************************** *********/
# Include <stdio. h>
# Define maxcol 10
# Define tabinc 8
Char line [maxcol];
Int exptab (int pos );
Int newpos (int pos );
Int findblnk (int pos );
Void printl (int pos );
Int main (void ){
Int C, Pos;
Pos = 0;
While (C = getchar ())! = EOF ){
Line [POS] = C; // stores the current character
If (C = '\ t') // determines whether it is a tab.
Pos = exptab (POS );
Else if (C = '\ n '){
Printl (POS); // if it is a line break such as C, print the entered character!
Pos = 0;
} Else if (++ POS> = maxcol ){
Pos = findblnk (POS );
Printl (POS );
Pos = newpos (POS );
}
}
}
// Define the printl Function
Void printl (int pos ){
Int I;
For (I = 0; I <Pos; ++ I)
Putchar (line [I]);
If (Pos> 0)
Putchar ('\ n ');
}
// Define the exptab Function
Int exptab (int pos ){
Line [POS] = '';
For (++ Pos; POS <maxcol & Pos % tabinc! = 0; ++ POS) // execute ++ POS first --> compare the size again -- >{}--> (;;++ POS) -- >{} -->
Line [POS] = '';
If (Pos <maxcol)
Return 0;
Else {
Printl (POS );
Return 0;
}
}
// Define the findblnk Function
Int findblnk (int pos ){
While (Pos> 0 & line [POS]! = '')
-- Pos;
If (Pos = 0)
Return maxcol;
Else
Return POS + 1;
}
// Define the newpos Function
Int newpos (int pos ){
Int I;
Int J;
If (Pos <= 0 | POS> = maxcol)
Return 0;
Else {
I = 0;
For (j = Pos; j <maxcol; ++ J ){
Line [I] = line [J];
++ I;
}
Return I;
}
}
Execution result:
650) This. length = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/8F/wKioL1QJg-CgKqrxAAFSIIfYrPo431.jpg "Title =" pjp.jpg "alt =" wKioL1QJg-CgKqrxAAFSIIfYrPo431.jpg "/> /********************* ************************
Maxcol is a symbolic constant that represents the row position of the input row, that is, column N of the input row.
The integer POS is the current position of the program in the text line. The program will fold the input row before column N at the first place of the input row.
The findblnk function reverts from the POS of the input row to find a space (to keep the word at the position of the line intact ).
If a space character is found, it returns the subscript next to the space character. If no space is found, it returns maxcol.
The function printl prints the characters before the pos-1 from position 0.
The newpos function is used to adjust the input line. It copies the characters starting from the position POs to the beginning of the next output line and then returns the new value of the POs variable.
**************************************** ***********/
Write a program to "fold" long input rows into two or more shorter rows.