開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料、light版)
- 言語: C
- コンパイラ: UNIX ccコンパイラ (汎用UNIX)
『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) II部(単純なプログラミング)の9章(変数スコープおよび関数)9.7(プログラミング実習)実習9-1を解いてみる。
実習9-1.
コード(TextWrangler)
#include <stdio.h>
#include <string.h>
int count(char string[]){
int index, words;
for(index = 0; index < strlen(string) ; index++){
switch(string[index]){
case ' ':
case ',':
case '.':
case '\n': words++;break;
case '\0': words++;break;
}
}
return (words);
}
int main(){
char line[100];
while(1){
printf("Enter line: ");
fgets(line,sizeof(line),stdin);
printf("語数: %d\n",count(line));
}
return (0);
}
入出力結果(Terminal)
$ cc -g -o sample sample.c $ ./sample Enter line: a b 語数: 2 Enter line: a b c d e 語数: 5 Enter line: c#,javascript,python,perl,c,ruby 語数: 6 Enter line: ^C $
0 コメント:
コメントを投稿