In article <11**********************@f14g2000cwb.googlegroups .com>,
"tiger786" <ya**********************@ssaglobal.com> wrote:
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
I would do it this way:
start with:
#include <string>
#include <iostream>
using namespace std;
string examine_sequence( const string& is ) {
string result;
// insert code here
return result;
}
void test_examine_sequence( const char* in, const char* out ) {
string esout = examine_sequence( in );
if ( esout != out ) {
cout << "Error: expected '" << out
<< "' for sequence '" << in
<< "', got '" << esout << "'" << endl;
throw -1;
}
}
int main() {
test_examine_sequence( "ccc", "0" );
test_examine_sequence( "ccdc", "1" );
test_examine_sequence( "ccdcdccc", "10" );
test_examine_sequence( "cccdcdc", "01" );
test_examine_sequence( "ccdcdcccdccc", "100" );
test_examine_sequence( "cccdcdcccc", "010" );
test_examine_sequence( "cccdcdccccddcdcdcb", "0101" );
cout << "everything works" << endl;
}
Run the above, fix any errors until "everything works" appears on the
screen. Then use this main:
int main() {
string s;
getline( cin, s );
cout << examine_sequence( s ) << endl;
}
If you have any trouble with fixing any of the errors. Post your
"examine_sequence" function and the error you are getting, and I'll help
you out.
BTW, this is called "test first design". It would do you well to look it
up.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.