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

001010001-invalid

Recommended Answers

All 2 Replies

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 {
	clrscr();
	printf("Enter the string: ");
	gets(string);
	if (check(string))
		printf("\n%s is accepted! ",string);
	else
		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},
					  {1,2},
					  {3,0},
					  {3,3}};

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

int isFinal(int state) {
	return (state == 3) ;
}
Member Avatar for iamthwee

Draw a flow chart and write some code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.