課題
80字より長い行を全て印字するプログラムを書け。
回答
/** * @file プログラミング言語C 1-17 * @brief * @author hiroyuki murai * @date 20171008 * @note */ #include <stdio.h> #include <limits.h> #include <string.h> #define MAX_LINE_NUM 1000 /* 入力行数の最大値 */ #define MAX_LENGTH 1000 /* 入力文字列長の最大値 */ const int MIN_COPY_LENGTH = 80; /* コピーする最低文字列長 */ typedef unsigned long long u8; int getline(char line[], u8 maxline); void copy(char to[], char from[]); /** * @fn Main * @brief * @param * @return * @detail */ int main(void) { u8 i, len, num_line = 0; // 行数 char line1[MAX_LENGTH]; // 保存元(読み取った文字列) char line2[MAX_LINE_NUM][MAX_LENGTH]; // 保存先 while ((len = getline(line1, MAX_LENGTH)) > 0) { if (strlen(line1) <= MIN_COPY_LENGTH) continue; // 規定文字数以下なら次の行へ copy(line2[num_line], line1); // 保存元から保存先へコピー num_line++; // 次の行へ if (num_line >= MAX_LINE_NUM) { // 保存先の行数を使い切ったら終了 break; } } printf("\n --- copy --- \n"); // タイトル for (i = 0; i < num_line; i++) { // 保存した行数だけ printf("%d:%s", strlen(line2[i]), line2[i]); // 保存先から取り出して印字 } return 0; } /* getline: sに行を入れる */ int getline(char s[], u8 lim) { int c; u8 i; 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; } /* 行コピーする */ void copy(char to[], char from[]) { u8 i; i = 0; while (( to[i] = from[i]) != '\0') { ++i; } } /* eof */
実行内容
This sentence will not be copyed.
Difference between nations so long as they do not lead to hostility, are by no means to be deplored.
Living for a time in a foreign countyr makes us aware of merits in which our own county is different.
This is dummy.
The same thing holds of differences is deficient.
実行結果
--- copy ---
101:Difference between nations so long as they do not lead to hostility, are by no means to be deplored.
102:Living for a time in a foreign countyr makes us aware of merits in which our own county is different.
所感
相変わらず言葉足らずな課題だ。