Selected solutions to exercise of "The C Programming Language" 2e (Part 6)
2006-02-19 23:16
639 查看
Exercise 1-22
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./*
KnR 1-22
----------
Write a program that wraps very long lines of input
into two or more shorter lines.
*/
#include <stdio.h>
#define MAXLINE 1000 /* max input line size */
#define FOLDLENGTH 80
/* current input line */
char line[MAXLINE];
/* taken from the KnR book. */
int getline(void);
/* print the line, from index of start to end */
void print(int start, int end);
/* find the last space in line from index of start to end */
int find_last_space(int start, int end);
int main()
{
int len;
while (( len = getline()) > 0 )
{
print(0, len);
}
return 0;
}
/* getline: specialized version */
int getline(void)
{
int c, i;
extern char line[];
for (i = 0;
i < MAXLINE-1 && (c = getchar()) != EOF && c != '/n'; ++i)
line[i] = c;
if(c == '/n')
{
line[i] = c;
++i;
}
line[i] = '/0';
return i;
}
int find_last_space(int start, int end) {
int place = -1;
int i = start;
int len = 1; /* the length from start to current postion */
for (; i < end && len <= FOLDLENGTH; i++, len++) {
if (line[i] == ' ') {
place = i;
}
}
return place;
}
void print(int start, int end) {
int len = end - start;
int place = -1;
int i = 0;
if (len <= FOLDLENGTH) {
for (i = start; i < end; i++)
printf ( "%c", line[i]);
return;
}
else {
/* find a proper space */
if ((place = find_last_space(start, end)) != -1 ) {
print(start, place + 1);
printf("/n");
print(place + 1, end);
} else {
print(start, start + FOLDLENGTH);
printf("/n");
print(start + FOLDLENGTH, end);
}
}
}
相关文章推荐
- Selected solutions to exercise of "The C Programming Language" 2e (Part 5)
- Selected solutions to exercise of "The C Programming Language" 2e (Part 7, End of Chapter 1)
- Selected solutions to exercise of "The C Programming Language" 2e (Part 1)
- Selected solutions to exercise of "The C Programming Language" 2e (Part 2)
- Selected solutions to exercise of "The C Programming Language" 2e (Part 3)
- Selected solutions to exercise of "The C Programming Language" 2e (Part 4)
- The C Programming Language——Exercise solutions of the chapter one (1st)
- "The C Programming Language", 2nd edition, Kernighan and Ritchie
- mt.exe : general error c101008d: Failed to write the updated manifest to the resource of file ".\Rel
- The meaning of "setting to Nothing"
- Source insight上修改samba共享ubuntu上内核源程序文件出现"Confirm by typing ‘yes' below"、"has been changed outside of the editor. Do you want to
- 当打开Oracle Jdevelopler 11g时出现"Unable to create an instace of the jave virtual Machine Located at path"错时解决办法
- The kayword "new" of CPP programming language
- bug:"Operation is not valid due to the current state of the object."
- Hudson/Jenkins -- 消除svn警告: "clock of the subversion server appears to be out of sync"
- 读书笔记之-----"The C Programming Language"
- How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
- 【.Net码农】关于使用FileUpload控件报 Could not find a part of the path "X/1.jpg"
- C Language Study - The importance of "Debug"
- Give a very good method to count the number of ones in a "n" (e.g. 32) bit number.