in java or C++ Write a high-level language program that will read strings of a\'
ID: 3562421 • Letter: I
Question
in java or C++
Write a high-level language program that will read strings of a's and b's of length 0 or more, 1 string per line of input. Using a finite-state, table-driven approach your program determines whether or not each line contains the substring babba. For each string, the program outputs the string then prints 'yes' if it contains an occurrence of babba and 'no' if it doesn't. Assume that all characters in the input string are either a or b. Do not use any built-in expression-matching utilities. Hint: begin by devising an FSM to recognize strings containing babbaExplanation / Answer
import java.util.Scanner;
public class search {
public static void main(String Args[])
{
String ipstr="",substr = "babba";
boolean blnfound=false;
System.out.println("enter string:");
Scanner scan = new Scanner(System.in);
ipstr = scan.nextLine();
for(int i=0;i<ipstr.length()-substr.length()+1;i++)
{
for(int j=0;j<substr.length();j++)
{
if(!ipstr.substring(i+j, i+j+1).equalsIgnoreCase(substr.substring(j,j+1)))
{
blnfound=false;
break;
}
else
{
blnfound=true;
}
}
if(blnfound)
break;
}
if(blnfound)
System.out.println("found");
else
System.out.println("not found");
}
}
A generalised code can also be written using recursion. reply if you want that
output
aaaaaaaaaaaaaaaaaababba
found
aaaaaaaaaaa
not found
enter string:
aababababababababababbab
found
enter string:
baaabaaabaaabbbbbbaaabaaaa
not found
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.