"tiger786" writes:
[color=blue]
>I am new to programming and studying basics in college .Can somebody
> write a program on this?
>
> The program should monitor a possibly infinite stream of characters
> from the keyboard (standard input). If it detects the sequence "ccc"
> it outputs a "0". If it detects the sequence "cdc" it outputs a "1".
> DO NOT detect sequences within sequences. The program should exit
> cleanly when it detects an End Of Input.
>
> For example:
>
> The following sequence ccdcdcccdccc<End Of Input> would produce the
> following result: 100
> While the following sequence cccdcdccccddcdcdcb<End Of Input> would
> produce the following result: 0101[/color]
Wow! What a nasty problem for an introductory course. The logical analysis
that precedes writing a program is the tough part. Be sure you get the
rules firmly in your head before you start. Write several test cases and
decide what the output should be with pencil and paper..
This is a problem from the wonderful world of finite state machines. If
you can find a nice tutorial it would be great, but I didn't find one in a
few moments with Google. It's frustrating to be unable to draw a simple
state machine in ASCII. I found this definition, but a picture would be a
thousand times better.
-----
FSMs are most commonly represented by state diagrams, which are also called
state transition diagrams. The state diagram is basically a directed graph
where each vertex represents a state and each edge represents a transition
between two states.
from
http://sakharov.net/fsmtutorial.html
----
Note well, however, that the state usually changes but does not *need* to
change, which conflicts with that definition.
If you can find a tutorial with three or four round circles and some lines
with arrowheads (mostly) connecting the circles you have hit pay dirt.
From reading the responses, I seem to be the only person in these newsgroups
that was born without an innate knowledge of state machines, perhaps you are
one of the lucky ones too..
Fortunately, the problem is simple enough that you can bull ahead without
knowing about state machines if you have to, and I suppose some might
consider using such as overkill..
Come up with some kind of algorithm, and write the code. I visualize the
centerpiece as a switch statement with four values, one for each of the
states. (If I didn't mess up.}