Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have uploaded that question 2 times , please someone solve it correctly A pref

ID: 3842321 • Letter: I

Question

I have uploaded that question 2 times , please someone solve it correctly

A prefix is a substring taken from the beginning of a word. "Mo" is a prefix (of length 2) of the word "Mother". Given a string, consider the prefix string made of the first N chars. Does that prefix string appear somewhere else in the string? Write a Java method that returns true if that prefix appear somewhere else in the string, and returns false if not. Your method should be case sensitive^1. Assume that the string is not empty and that 1

Explanation / Answer

PROGRAM CODE:

import java.util.*;
import java.lang.*;
import java.io.*;

public class PrefixChecker
{
   public static boolean prefixAgain(String str, int n)
   {
       String prefix = str.substring(0, n);
       String sub = str.substring(n);
       if(sub.contains(prefix))
           return true;
       else
       return false;
   }
  
   public static void main (String[] args) throws java.lang.Exception
   {
       System.out.println(prefixAgain("abXYabc", 1));
       System.out.println(prefixAgain("abXYabc", 2));
       System.out.println(prefixAgain("abXYabc", 3));
       System.out.println(prefixAgain("aBXYab", 2));
   }
}

OUTPUT:

true
true
false
false