開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第1章(やさしい入門)、1.9(文字配列)の演習 1-16、1-17、1-18、1-19を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 1-16.
コード
sample.c
#include <stdio.h> #define MAXLINE 1000 int my_getline(char line[], int maxline); void copy(char to[], char from[]); int main() { int len; int max; char line[MAXLINE]; char longest[MAXLINE]; max = 0; while ((len = my_getline(line, MAXLINE)) > 0) { printf("%d, %s", len, line); if (len > max) { max = len; copy(longest, line); } } if (max > 0) printf("%s", longest); return 0; } int my_getline(char s[], int lim) { int c, i, j; j = 0; for (i = 0; (c = getchar()) != EOF && c !='\n'; ++i) if (i < lim - 2) { s[j] = c; ++j; } if (c == '\n') { s[j] = c; ++i; ++j; } s[j] = '\0'; return i; } void copy(char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; }
入出力結果(Terminal)
$ cat sample.c | ./a.out 19, #include <stdio.h> 21, #define MAXLINE 1000 1, 42, int my_getline(char line[], int maxline); 35, void copy(char to[], char from[]); 1, 11, int main() 2, { 13, int len; 13, int max; 24, char line[MAXLINE]; 27, char longest[MAXLINE]; 5, 13, max = 0; 52, while ((len = my_getline(line, MAXLINE)) > 0) { 37, printf("%d, %s", len, line); 25, if (len > max) { 23, max = len; 33, copy(longest, line); 10, } 6, } 17, if (max > 0) 31, printf("%s", longest); 14, return 0; 2, } 1, 34, int my_getline(char s[], int lim) 2, { 17, int c, i, j; 5, 11, j = 0; 58, for (i = 0; (c = getchar()) != EOF && c !='\n'; ++i) 27, if (i < lim - 2) { 22, s[j] = c; 17, ++j; 10, } 21, if (c == '\n') { 18, s[j] = c; 13, ++i; 13, ++j; 6, } 17, s[j] = '\0'; 14, return i; 2, } 1, 34, void copy(char to[], char from[]) 2, { 11, int i; 5, 11, i = 0; 38, while ((to[i] = from[i]) != '\0') 13, ++i; 2, } for (i = 0; (c = getchar()) != EOF && c !='\n'; ++i) $ ./a.out 1 2, 1 12345 6, 12345 12 3, 12 1234 5, 1234 123 4, 123 12345 6, 12345 67890 6, 67890 12345 $ ./a.out 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 1000, 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 1001, 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 $
演習 1-17.
コード
sample.c
#include <stdio.h> #define MAXLINE 1000 #define MINLENGTH 80 int my_getline(char line[], int maxline); int main() { int len; char line[MAXLINE]; while ((len = my_getline(line, MAXLINE)) > 0) if (len > MINLENGTH) printf("%s", line); return 0; } int my_getline(char s[], int lim) { int c, i, j; j = 0; for (i = 0; (c = getchar()) != EOF && c !='\n'; ++i) if (i < lim - 2) { s[j] = c; ++j; } if (c == '\n') { s[j] = c; ++i; ++j; } s[j] = '\0'; return i; }
入出力結果(Terminal)
$ ./a.out 1234677890 12346778901234677890 123467789012346778901234677890 1234677890123467789012346778901234677890 12346778901234677890123467789012346778901234677890 123467789012346778901234677890123467789012346778901234677890 1234677890123467789012346778901234677890123467789012346778901234677890 12346778901234677890123467789012346778901234677890123467789012346778901234677890 12346778901234677890123467789012346778901234677890123467789012346778901234677890 1234677890123467789012346778901234677890123467789012346778901234677890123467789 123467789012346778901234677890123467789012346778901234677890123467789012346778901 123467789012346778901234677890123467789012346778901234677890123467789012346778901 123467789012346778901234677890123467789012346778901234677890123467789012346778901234677890 123467789012346778901234677890123467789012346778901234677890123467789012346778901234677890 $ cat sample.c|./a.out
演習 1-18.
コード
sample.c
#include <stdio.h> #define MAXLINE 1000 int my_getline(char line[], int maxline); int my_remove(char line[]); int main() { char line[MAXLINE]; while (my_getline(line, MAXLINE) > 0) if (my_remove(line) > 0) printf("%s", line); return 0; } int my_getline(char s[], int lim) { int c, i, j; j = 0; for (i = 0; (c = getchar()) != EOF && c !='\n'; ++i) if (i < lim - 2) { s[j] = c; ++j; } if (c == '\n') { s[j] = c; ++i; ++j; } s[j] = '\0'; return i; } int my_remove(char line[]) { int c, i; i = 0; while (line[i] != '\n') ++i; --i; while ( i >= 0 && (line[i] == ' ' || line[i] == '\t')) --i; if (i >= 0) { ++i; line[i] = '\n'; ++i; line[i] = '\0'; } return i; }
入出力結果(Terminal)
$ cat sample.c | ./a.out #include <stdio.h> #define MAXLINE 1000 int my_getline(char line[], int maxline); int my_remove(char line[]); int main() { char line[MAXLINE]; while (my_getline(line, MAXLINE) > 0) if (my_remove(line) > 0) printf("%s", line); return 0; } int my_getline(char s[], int lim) { int c, i, j; j = 0; for (i = 0; (c = getchar()) != EOF && c !='\n'; ++i) if (i < lim - 2) { s[j] = c; ++j; } if (c == '\n') { s[j] = c; ++i; ++j; } s[j] = '\0'; return i; } int my_remove(char line[]) { int c, i; i = 0; while (line[i] != '\n') ++i; --i; while ( i >= 0 && (line[i] == ' ' || line[i] == '\t')) --i; if (i >= 0) { ++i; line[i] = '\n'; ++i; line[i] = '\0'; } return i; } $
演習 1-19.
コード
sample.c
#include <stdio.h> #define MAXLINE 1000 int my_getline(char line[], int maxline); void reverse(char line[]); int main() { char line[MAXLINE]; while (my_getline(line, MAXLINE) > 0) { reverse(line); printf("%s", line); } return 0; } int my_getline(char s[], int lim) { int c, i, j; j = 0; for (i = 0; (c = getchar()) != EOF && c !='\n'; ++i) if (i < lim - 2) { s[j] = c; ++j; } if (c == '\n') { s[j] = c; ++i; ++j; } s[j] = '\0'; return i; } void reverse(char line[]) { int c, i, j; char tmp_c; i = 0; while (line[i] != '\0') ++i; --i; if (line[i] == '\n') --i; j = 0; while (j < i) { tmp_c = line[j]; line[j] = line[i]; line[i] = tmp_c; ++j; --i; } }
入出力結果(Terminal)
$ cat sample.c | ./a.out >h.oidts< edulcni# 0001 ENILXAM enifed# ;)enilxam tni ,][enil rahc(enilteg_ym tni ;)][enil rahc(esrever diov )(niam tni { ;]ENILXAM[enil rahc { )0 > )ENILXAM ,enil(enilteg_ym( elihw ;)enil(esrever ;)enil ,"s%"(ftnirp } ;0 nruter } )mil tni ,][s rahc(enilteg_ym tni { ;j ,i ,c tni ;0 = j )i++ ;'n\'=! c && FOE =! ))(rahcteg = c( ;0 = i( rof { )2 - mil < i( fi ;c = ]j[s ;j++ } { )'n\' == c( fi ;c = ]j[s ;i++ ;j++ } ;'0\' = ]j[s ;i nruter } )][enil rahc(esrever diov { ;j ,i ,c tni ;c_pmt rahc ;0 = i )'0\' =! ]i[enil( elihw ;i++ ;i-- )'n\' == ]i[enil( fi ;i-- ;0 = j { )i < j( elihw ;]j[enil = c_pmt ;]i[enil = ]j[enil ;c_pmt = ]i[enil ;j++ ;i-- } } $ ./a.out 12345 54321 abcde edcba $
0 コメント:
コメントを投稿