Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Set 1
- 1 ans:-
- #include <iostream>
- #include <string.h>
- #include <stdlib.h>
- using namespace std;
- void substring1(char w[25],int a,int b);
- void substring2(char word[100],int c,int d);
- int main()
- {
- char w[100],word[100];
- int st1,st2,ed1,ed2;
- cout<<"Enter a string: ";
- cin>>w;
- cout<<"Enter Starting Position: ";
- cin>>st1;
- cout<<"Enter Ending Position: ";
- cin>>ed1;
- if(strlen(w)<ed1)
- {
- cout<<"\nError: Word Is shorter than the ending position specified";
- exit(0);
- }
- else
- {
- cout<<endl;
- substring1(w,st1,ed1);
- }
- cout<<"\n\n"<<endl;
- return 0;
- }
- void substring1(char w[25],int a,int b){
- int i;
- cout<<"Substring is ";
- for(i=a;i<=b;i++){
- cout<<w[i-1];
- }
- }
- 2. ans:-
- #include<iostream>
- #include<string.h>
- using namespace std;
- int current=0;
- int delta(int current,char symbol);
- int main()
- {
- int i=0;
- int a[100];
- string w;
- cout<<"Enter string: ";
- cin>>w;
- while(w[i]!='\0')
- {
- current=delta(current,w[i]);
- i++;
- }
- if(current==2)
- cout<<w<<" is accepted";
- else
- cout<<w<<" is rejeced";
- return 0;
- }
- int delta(int current, char symbol)
- {
- switch(current)
- {
- case 0:
- if(symbol=='0')
- {
- current=1;
- }
- else
- {
- current=3;
- }
- break;
- case 1:
- if(symbol=='0')
- {
- current=3;
- }
- else
- {
- current=2;
- }
- break;
- case 2:
- if(symbol=='0')
- {
- current=2;
- }
- else
- {
- current=2;
- }
- break;
- }
- return current;
- }
- 3. ans:-
- #include<iostream>
- #include<string.h>
- using namespace std;
- int current_state;
- void p(char ip,char ipp){
- if(ip=='0'){
- if(ipp=='1'){
- current_state=1;
- }else{
- current_state=0;
- }
- }else{
- current_state=0;
- }
- }
- void q(char ip){
- if(ip=='1'){
- current_state=2;
- }
- }
- int NFA_end(string w){
- int i, l;
- l=w.length();
- current_state=0;
- for(i=0;i<l;i++){
- if(current_state==0){
- p(w[i],w[i+1]);
- }
- else if(current_state==1){
- q(w[i]);
- }
- else{
- current_state=0;
- i--;
- }
- }
- return current_state;
- }
- int main(){
- string w;
- cout<<"Enter string: ";
- cin>>w;
- current_state=NFA_end(w);
- if(current_state==2){
- cout<<w <<" is accepted";
- }else{
- cout<<w <<" is rejected";
- }
- }
- Set 2
- 1. ans:-
- #include <iostream>
- #include <string.h>
- #include <stdlib.h>
- using namespace std;
- void substring1(char w[25],int a,int b);
- void substring2(char word[100],int c,int d);
- int main()
- {
- char w[100],word[100];
- int st1,st2,ed1,ed2;
- cout<<"Enter a string: ";
- cin>>word;
- cout<<"Enter Starting Position: ";
- cin>>st2;
- cout<<"Enter No. Of Symbols To Be Extracted: ";
- cin>>ed2;
- if(strlen(word)<ed2){
- cout<<"\nError: Word Is shorter than the symbols to be extracted specified";
- exit(0);
- }else{
- cout<<endl;
- substring2(word,st2,ed2);
- }
- return 0;
- }
- void substring2(char word[100],int c,int d){
- int i;
- cout<<"Substring is ";
- for(i=c;i<=d+1;i++){
- cout<<word[i-1];
- }
- cout<<endl;
- }
- 3. ans:-
- #include<iostream>
- #include<string.h>
- using namespace std;
- int current_state;
- void p(char ip,char ipp){
- if(ip=='0'){
- if(ipp=='0'){
- current_state=1;
- }else{
- current_state=0;
- }
- }else{
- current_state=0;
- }
- }
- void q(char ip){
- if(ip=='0'){
- current_state=2;
- }
- }
- void r(char ip){
- if(ip=='1'){
- current_state=3;
- }
- }
- void s(char ip){
- if(ip=='0'){
- current_state=3;
- }else{
- current_state=3;
- }
- }
- int NFA_substring(string w){
- int i, l;
- l=w.length();
- current_state=0;
- for(i=0;i<l;i++)
- {
- if(current_state==0){
- p(w[i],w[i+1]);
- }
- else if(current_state==1){
- q(w[i]);
- }
- else if(current_state==2){
- r(w[i]);
- }
- else if(current_state==3){
- s(w[i]);
- }
- }
- return current_state;
- }
- int main()
- {
- string w;
- cout<<"Enter string:";
- cin>>w;
- current_state=NFA_substring(w);
- if(current_state==3){
- cout<<w <<" is accepted";
- }
- else{
- cout<<w <<" is rejected";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement