開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第1章(やさしい入門)、1.5(文字入出力)、1.5.2(文字のカウント)、1.5.4(単語のカウント)の演習1-12を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 1-12.
コード
sample.c
#include <stdio.h>
#define IN 1
#define OUT 0
int main()
{
int c, state;
state = OUT;
while ((c = getchar()) != EOF) {
if (c != '\n' && c != ' ' && c != '\t') {
state = IN;
putchar(c);
} else if (state == IN) {
putchar('\n');
state = OUT;
}
}
if (state == IN) {
putchar('\n');
}
return 0;
}
入出力結果(Terminal)
$ ./a.out < sample.c
#include
<stdio.h>
#define
IN
1
#define
OUT
0
int
main()
{
int
c,
state;
state
=
OUT;
while
((c
=
getchar())
!=
EOF)
{
if
(c
!=
'\n'
&&
c
!=
'
'
&&
c
!=
'\t')
{
state
=
IN;
putchar(c);
}
else
if
(state
==
IN)
{
putchar('\n');
state
=
OUT;
}
}
if
(state
==
IN)
{
putchar('\n');
}
return
0;
}
$ cat sample.txt
Ah Love! could you and I with Fate conspire
To grasp this sorry Scheme of Things entire,
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
ah love! could you and i with fate conspire
to grasp this sorry scheme of things entire,
would not we shatter it to bits -- and then
re-mould it nearer to the heart's desire!
!@#$%Ah Love! could you and I with Fate conspire
!@#$%To grasp this sorry Scheme of Things entire,
!@#$%Would not we shatter it to bits -- and then
!@#$%Re-mould it nearer to the Heart's Desire!
!@#$%ah love! could you and i with fate conspire
!@#$%to grasp this sorry scheme of things entire,
!@#$%would not we shatter it to bits -- and then
!@#$%re-mould it nearer to the heart's desire!
$ ./a.out < sample.txt
Ah
Love!
could
you
and
I
with
Fate
conspire
To
grasp
this
sorry
Scheme
of
Things
entire,
Would
not
we
shatter
it
to
bits
--
and
then
Re-mould
it
nearer
to
the
Heart's
Desire!
ah
love!
could
you
and
i
with
fate
conspire
to
grasp
this
sorry
scheme
of
things
entire,
would
not
we
shatter
it
to
bits
--
and
then
re-mould
it
nearer
to
the
heart's
desire!
!@#$%Ah
Love!
could
you
and
I
with
Fate
conspire
!@#$%To
grasp
this
sorry
Scheme
of
Things
entire,
!@#$%Would
not
we
shatter
it
to
bits
--
and
then
!@#$%Re-mould
it
nearer
to
the
Heart's
Desire!
!@#$%ah
love!
could
you
and
i
with
fate
conspire
!@#$%to
grasp
this
sorry
scheme
of
things
entire,
!@#$%would
not
we
shatter
it
to
bits
--
and
then
!@#$%re-mould
it
nearer
to
the
heart's
desire!
$
0 コメント:
コメントを投稿