Advertisement
DevilVeNom

TOC

Nov 8th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. Set 1
  2.  
  3. 1 ans:-
  4.  
  5. #include <iostream>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. using namespace std;
  9. void substring1(char w[25],int a,int b);
  10. void substring2(char word[100],int c,int d);
  11. int main()
  12. {
  13.  char w[100],word[100];
  14.  int st1,st2,ed1,ed2;
  15.  cout<<"Enter a string: ";
  16.  cin>>w;
  17.  cout<<"Enter Starting Position: ";
  18.  cin>>st1;
  19.  cout<<"Enter Ending Position: ";
  20.  cin>>ed1;
  21.  if(strlen(w)<ed1)
  22.  {
  23.  cout<<"\nError: Word Is shorter than the ending position specified";
  24.  exit(0);
  25.  }
  26.  else
  27.  {
  28.  cout<<endl;
  29.  substring1(w,st1,ed1);
  30.  }
  31.  cout<<"\n\n"<<endl;
  32.  return 0;
  33. }
  34. void substring1(char w[25],int a,int b){
  35.  int i;
  36.  cout<<"Substring is ";
  37.  for(i=a;i<=b;i++){
  38.  cout<<w[i-1];
  39.  }
  40. }
  41.  
  42. 2. ans:-
  43.  
  44. #include<iostream>
  45. #include<string.h>
  46. using namespace std;
  47. int current=0;
  48. int delta(int current,char symbol);
  49. int main()
  50. {
  51. int i=0;
  52. int a[100];
  53. string w;
  54. cout<<"Enter string: ";
  55. cin>>w;
  56. while(w[i]!='\0')
  57. {
  58. current=delta(current,w[i]);
  59. i++;
  60. }
  61. if(current==2)
  62. cout<<w<<" is accepted";
  63. else
  64. cout<<w<<" is rejeced";
  65. return 0;
  66. }
  67. int delta(int current, char symbol)
  68. {
  69. switch(current)
  70. {
  71. case 0:
  72. if(symbol=='0')
  73. {
  74. current=1;
  75. }
  76. else
  77. {
  78. current=3;
  79. }
  80. break;
  81. case 1:
  82. if(symbol=='0')
  83. {
  84. current=3;
  85. }
  86. else
  87. {
  88. current=2;
  89. }
  90. break;
  91. case 2:
  92. if(symbol=='0')
  93. {
  94. current=2;
  95. }
  96. else
  97. {
  98. current=2;
  99. }
  100. break;
  101. }
  102. return current;
  103. }
  104.  
  105. 3. ans:-
  106.  
  107. #include<iostream>
  108. #include<string.h>
  109. using namespace std;
  110. int current_state;
  111. void p(char ip,char ipp){
  112. if(ip=='0'){
  113. if(ipp=='1'){
  114. current_state=1;
  115. }else{
  116. current_state=0;
  117. }
  118. }else{
  119. current_state=0;
  120. }
  121. }
  122. void q(char ip){
  123. if(ip=='1'){
  124. current_state=2;
  125. }
  126. }
  127. int NFA_end(string w){
  128. int i, l;
  129. l=w.length();
  130. current_state=0;
  131. for(i=0;i<l;i++){
  132. if(current_state==0){
  133. p(w[i],w[i+1]);
  134. }
  135. else if(current_state==1){
  136. q(w[i]);
  137. }
  138. else{
  139. current_state=0;
  140. i--;
  141. }
  142. }
  143. return current_state;
  144. }
  145. int main(){
  146. string w;
  147. cout<<"Enter string: ";
  148. cin>>w;
  149. current_state=NFA_end(w);
  150. if(current_state==2){
  151. cout<<w <<" is accepted";
  152. }else{
  153. cout<<w <<" is rejected";
  154. }
  155. }
  156.  
  157.  
  158.  
  159. Set 2
  160.  
  161. 1. ans:-
  162.  
  163. #include <iostream>
  164. #include <string.h>
  165. #include <stdlib.h>
  166. using namespace std;
  167. void substring1(char w[25],int a,int b);
  168. void substring2(char word[100],int c,int d);
  169. int main()
  170. {
  171.  char w[100],word[100];
  172.  int st1,st2,ed1,ed2;
  173.  cout<<"Enter a string: ";
  174.  cin>>word;
  175.  cout<<"Enter Starting Position: ";
  176.  cin>>st2;
  177.  cout<<"Enter No. Of Symbols To Be Extracted: ";
  178.  cin>>ed2;
  179.  if(strlen(word)<ed2){
  180.  cout<<"\nError: Word Is shorter than the symbols to be extracted specified";
  181.  exit(0);
  182.  }else{
  183.  cout<<endl;
  184.  substring2(word,st2,ed2);
  185.  }
  186.  return 0;
  187. }
  188. void substring2(char word[100],int c,int d){
  189.  int i;
  190.  cout<<"Substring is ";
  191.  for(i=c;i<=d+1;i++){
  192.  cout<<word[i-1];
  193.  }
  194.  cout<<endl;
  195. }
  196.  
  197. 3. ans:-
  198.  
  199. #include<iostream>
  200. #include<string.h>
  201. using namespace std;
  202. int current_state;
  203. void p(char ip,char ipp){
  204. if(ip=='0'){
  205. if(ipp=='0'){
  206. current_state=1;
  207. }else{
  208. current_state=0;
  209. }
  210. }else{
  211. current_state=0;
  212. }
  213. }
  214. void q(char ip){
  215. if(ip=='0'){
  216. current_state=2;
  217. }
  218. }
  219. void r(char ip){
  220. if(ip=='1'){
  221. current_state=3;
  222. }
  223. }
  224. void s(char ip){
  225. if(ip=='0'){
  226. current_state=3;
  227. }else{
  228. current_state=3;
  229. }
  230. }
  231. int NFA_substring(string w){
  232. int i, l;
  233. l=w.length();
  234. current_state=0;
  235. for(i=0;i<l;i++)
  236. {
  237. if(current_state==0){
  238. p(w[i],w[i+1]);
  239. }
  240. else if(current_state==1){
  241. q(w[i]);
  242. }
  243. else if(current_state==2){
  244. r(w[i]);
  245. }
  246. else if(current_state==3){
  247. s(w[i]);
  248. }
  249. }
  250. return current_state;
  251. }
  252. int main()
  253. {
  254. string w;
  255. cout<<"Enter string:";
  256. cin>>w;
  257. current_state=NFA_substring(w);
  258. if(current_state==3){
  259. cout<<w <<" is accepted";
  260. }
  261. else{
  262. cout<<w <<" is rejected";
  263. }
  264. }
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement