You are on page 1of 3

ASSIGNMENT 1

THEORY OF AUTOMATA
TASK1
#include <iostream>
#include <string>
using namespace std;

int main() {

string str1;
cout<<"Enter a string"<<endl;
cin>> str1;
int len =str1.size();

string word;
string word2;
int flag1=0;

for(int i=0;i<len;i++){
char c1=str1[i];
char c2=str1[i+1];
char c3=str1[i+2];
word= string(1, c1) + string(1, c2) + string(1, c3);
word2=string(1, c1) + string(1, c2);
if (word=="baa" or word2=="ab")
{cout<<"valid word"<<endl;
flag1++;
break;
}

if (flag1 == 0)
{cout<<"invalid word";}

}
RESULT

TASK2
#include<iostream>
using namespace std;
int main(){

string str1;
cout<<"Enter a string "<<endl;
cin>>str1;

int len=str1.size();
if ((str1[0]=='a' and str1[len-1]=='b') || ((str1[0]=='b' and str1[len-
1]=='a')))
cout<<"valid word";
else
cout<<"invalid word";

}
RESULT

You might also like