C語言實現代碼如下:
#include<stdio.h>
#define N 1000
void main()
{
char state='A';
char ch[N];
int n,m;
printf("請輸入由 a和b字母組成的字符串:/n");
gets(ch);
for(n=0;ch[n];n++)
{
if(ch[n]=='a'|| ch[n]=='b')
{
s: switch(state)
{
case 'A': switch(ch[n]){
case 'a': state='B'; break;
case 'b': state='A'; continue;
} break;
case 'B': switch(ch[n]){
case 'a': state='B'; continue;
case 'b': state='D'; break;
} break;
case 'C': switch(ch[n]){
case 'a': state='B'; break;
case 'b': state='A'; break;
} break;
case 'D':goto h;
}
}
else if(ch[n]==' ')
{
for(m=n+1;ch[m]!='/0';m++)
{
ch[m-1]=ch[m];
}
for(n=0;ch[n];n++)
ch[n]=ch[m];
goto s;
}
else
printf("please input a||b/n");
}
h: if( (state=='D')&&(ch[n--]=='/0') )
printf("可被接受/n");
else
printf("不可被接受/n");
}