voidtemperature_convert(int fahr, int upper, int step){ int celsius; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } }
1-16: 打印任意长度输出行
#include<stdio.h> #define MAXLINE 1000 /*maximum input line length*/
intget_line(char line[], int maxline);
/* print input line and length */ intmain() { int len; /* current line length */ char line[MAXLINE]; /* current input line */ while ((len = get_line(line, MAXLINE)) > 0) printf("Length: %d \nline: %s", len, line); return0; }
/* getline: read a line into s, return length */ intget_line(char s[],int lim) { int c, i; c = 0; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i-1; }
1-17: 打印大于长度80的行
#include<stdio.h> #define MAXLINE 1000 /*maximum input line length*/
intget_line(char line[], int maxline);
/* print input line and length */ intmain() { int len; /* current line length */ char line[MAXLINE]; /* current input line */ while ((len = get_line(line, MAXLINE)) > 0) if(len > 80) printf("%s", line); return0; }
/* getline: read a line into s, return length */ intget_line(char s[],int lim) { int c, i; c = 0; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i-1; }
/* copy: copy 'from' into 'to'; assume to is big enough */ voidcopy_then_print_length(char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; printf("Length: %d", i); }
1-18: 删除行末尾的空白和全为空白字符的行
#include<stdio.h> #define MAXLINE 10000 /*maximum input text length*/
voidget_text(char text[], int maxline); voiddelete_space(char text[]);
/* getline: read text into s */ voidget_text(char s[],int lim) { int c, i; c = 0; for (i=0; i < lim-1 && (c=getchar())!=EOF; ++i) s[i] = c; s[i] = '\0';
}
/* 删除每行后多余的空白字符(就地修改) */ voiddelete_space(char text[]) { int i, j, space; /* i, j 双指针,space用于记录行尾空白字符(除换行符)的数量,以更新 j 的位置 */ /* 利用 j 对 text[] 进行就地修改 */ for (i = j = space = 0; text[i] != '\0'; ++i, ++j) { if (text[i] == '\n') { if (j == 0) /* 防止数组在下面的判断中越界 */ { --j; continue; } elseif (text[i] == text[j-1]) /* 判断是否是连续的空行 */ --j; elseif (space != 0) /* 判断换行符前是否有空白,其实 space != 0 可以删去,加上便于理解 */ j -= space; } if (text[i] == ' ' || text[i] == '\t') ++space; /* 下一次循环起作用 */ else space = 0; text[j] = text[i]; } text[j] = '\0'; }
1-19: 颠倒每行的字符
#include<stdio.h> #define MAXLINE 10000 /*maximum input text length*/
voidget_text(char text[], int maxline); voidreverse(char text[], int i, int j);
intmain() { char text[MAXLINE]; /* current input text */ get_text(text, MAXLINE); printf("%s", text); return0; }
/* getline: read text into s */ voidget_text(char s[],int lim) { int c, i, j; /* i, j 记录当前行首和行尾的位置 */ c = 0; for (i=0, j=0; i < lim-1 && (c=getchar())!=EOF; ++i) { s[i] = c; if (c == '\n') { reverse(s, i-1, j); j = i + 1; } } s[i] = '\0';
}
/* 颠倒顺序 */ voidreverse(char text[], int i, int j) { int temp; while (i > j) { temp = text[i]; text[i] = text[j]; text[j] = temp; --i; ++j; } }
1-20: tab转space
#include<stdio.h> #define MAXLINE 10000 /*maximum input text length*/ #define TSPACE 4 /* Tab = 4 space */
voidget_text(char text[], int maxline); intdetab(char s[], int i);
intmain() { char text[MAXLINE]; /* current input text */ get_text(text, MAXLINE); printf("%s", text); return0; }
/* get_text: read text into s */ voidget_text(char s[],int lim) { int c, i; c = 0; for (i=0; i < lim-1 && (c=getchar())!=EOF; ++i) { s[i] = c; if (c == '\t') i = detab(s, i); } s[i] = '\0';