Construct a program that reads a string of characters from e

Construct a program that reads a string of characters from either \'a\' or \'b\' or \'c\' to solve whether the string of input is a member of...

We discussed how a pushdown automaton with two stacks would be able to accept strings in the non-context-free language AnBnCn (\"n a\'s followed by n b\'s followed by n c\'s\"). Algorithm Sketch: read a\'s and push them onto stack1, while reading b\'s pop stack1 for each b and push one b onto stack2; while reading c\'s, pop stack2; the string is accepted when the string is completely read and stack1 and stack2 are empty, otherwise, the string is not in AnBnCn. (Work out the finer points by thinking through the various scenarios of mismatched numbers of a\'s, b\'s, and c\'s, and unacceptable permutations of these symbols. This will help prevent undesirable run-time behaviors/crashes of your program e.g., attempts to pop an empty stack.) In a programming language of your choice (likely C++, possibly Python, or Java, or write a simple program that reads a string of characters in Ha,b,c), once from left to right, and uses two stacks in order to determine whether or not the input string is a member in AnBncn. Note: You are not asked to write a program that \"simulates\" the process of a formal PDA as we have studied I e, your program will not read next state/stack configurations off a transition table It must correct identify membership or non-membership for all 64+4 sample inputs listed below. It must do so without any counting of frequencies for symbols \'a\', \'b\', and \'c. It must use two stacks (either use an explicit stack data structure or use another suitable data structure in stack fashion.) Your program must feature a Boolean function which determines for a given string whether or not it is in AnBncn. (No \"big main()\" without functions!)

Solution

#include <stdio.h> #include <string.h> int main(){ char string1[20]; int i, length; int flag = 0; printf(\"Enter a string:\"); scanf(\"%s\", string1); length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; } } if (flag) { printf(\"%s is not a palindrome\", string1); } else { printf(\"%s is a palindrome\", string1); } return 0; }#include <stdio.h> #include <string.h> int main(){ char string1[20]; int i, length; int flag = 0; printf(\"Enter a string:\"); scanf(\"%s\", string1); length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; } } if (flag) { printf(\"%s is not a palindrome\", string1); } else { printf(\"%s is a palindrome\", string1); } return 0; }#include <stdio.h> #include <string.h> int main(){ char string1[20]; int i, length; int flag = 0; printf(\"Enter a string:\"); scanf(\"%s\", string1); length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; } } if (flag) { printf(\"%s is not a palindrome\", string1); } else { printf(\"%s is a palindrome\", string1); } return 0; }#include <stdio.h> #include <string.h> int main(){ char string1[20]; int i, length; int flag = 0; printf(\"Enter a string:\"); scanf(\"%s\", string1); length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; } } if (flag) { printf(\"%s is not a palindrome\", string1); } else { printf(\"%s is a palindrome\", string1); } return 0; }#include <stdio.h> #include <string.h> int main(){ char string1[20]; int i, length; int flag = 0; printf(\"Enter a string:\"); scanf(\"%s\", string1); length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; } } if (flag) { printf(\"%s is not a palindrome\", string1); } else { printf(\"%s is a palindrome\", string1); } return 0; }

Construct a program that reads a string of characters from either \'a\' or \'b\' or \'c\' to solve whether the string of input is a member of... We discussed ho

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site