開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料、light版)
- 言語: C
- コンパイラ: UNIX ccコンパイラ (汎用UNIX)
『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) の9章(変数スコープおよび関数)9.7(プログラミング実習)実習1を解いてみる。
実践9-1.
正規表現の単語境界「\b」を使えないし、「語」数っていうのがいまいち正確にわからないのでとりあえず単語数ということにしてスペースの数で単語数を判断することに。
コード(TextWrangler)
#include <stdio.h> #include <string.h> int main(){ char line[1000]; int count; int i; while(1){ printf("文字列を入力: "); fgets(line, sizeof(line),stdin); count = 0; for( i = 0 ; i < strlen(line) ; ++i){ if(line[i] == ' '){ count += 1; } } if(strlen(line) != 1){ count += 1; } printf("語数: %d語\n", count); } return (0); }
入出力結果(Terminal)
$ cc -g -o sample sample.c $ ./sample 文字列を入力: 語数: 0語 文字列を入力: Hello, World! 語数: 2語 文字列を入力: hi 語数: 1語 文字列を入力: a bc def ghij kl 語数: 5語 文字列を入力: ^C $
0 コメント:
コメントを投稿