Split from - http://www.daniweb.com/forums/thread34812-2.html

hi can you help me with my problem in c++ coding?
my problem that in every two zeros it must fallowed by 1, to become valid
if the input is not 001 like this it becomes invalid.....

example: 001001001 - valid


Edited by __avd: Split into a new thread.

7 Years
Discussion Span
Last Post by iamthwee

this program will search the "aba"
if the input word is abad something like this it becomes valid. if aba is not in the first 3 letters in the word it becomes invalid

MY problem is that how can i change the rule of this program to become "001" . if i input 001 or 001001 it become valid
if i input 001010 or 101001 it becomes invalid.
" this is the problem word - in every two zeros it must fallowed by 1."

# include <stdio.h>
# include <conio.h>

int getInputCode(char ch);
int check(char *st);
int isFinal(int state);

void main() {

 char string[80];

 do {
	printf("Enter the string: ");
	if (check(string))
		printf("\n%s is accepted! ",string);
		printf("\n%s is not accepted!",string);
	printf("\n\nPress q to quit.. ");
 } while (getch() != 'q');


int getInputCode(char ch) {  // in this case i cant understand what hpn.
  int code = 0;
  switch (ch) {
	case 'a': code = 0; break;
	case 'b': code = 1; break;
  return code;

int check(char *st) {
  int j=0, state = 0,input = 0;
  int table[4][2] = { {1,0},

  while (st[j] != '\0') {
	input = getInputCode(st[j]);
	state = table[state][input];
	if (state == 3)
  return isFinal(state);

int isFinal(int state) {
	return (state == 3) ;

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.