Question

in java!! Write a method to check prefix of two strings that have more than 3...

in java!!

Write a method to check prefix of two strings that have more than 3 characters. A 3-char prefix of a word “class” will be “cla”. The method should take two strings as parameters, then compare 3-char prefix of two strings.


Write a method to check suffix of two strings that have more than 3 characters. A 3-char suffix of a word “world” will be “rld”. The method should take two strings as parameters, then compare 3-char suffix of two strings.


Detail requirements of the methods are below:


Requirements:


  1. Use Scanner object to take strings input from console.

  2. Both methods should check the length of the input strings first. If any string is less than 3 characters, the method should show “String too short, please re-enter.” And ask inputs again until both strings equal or more than 3 characters.

  3. If prefix or suffix matches, it should show “3-char prefix matched” or “3-char suffix matched”, and also show the matched 3 characters.

  4. If prefix or suffix does not match, it should show “3-char prefix didn’t match” or “3-char suffix didn’t match”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code -

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   //variable declare
   String first, second;
   Scanner sc = new Scanner(System.in);
   do{
   //ask user to enter first string
       System.out.println("Enter first string");
       first = sc.next();
   //ask user to enter second string
       System.out.println("Enter second string");
       second = sc.next();
   }while(first.length()<3&&second.length()<3);
   //call function to check the prefix are equal or not
       checkPrefix(first,second);
   //call function to check the suffix are equal or not
   checkSuffix(first,second);
      
   }
   public static void checkPrefix(String a, String b){
   //get the first 3 char of string
   String first = a.substring(0, 3);
   String second = b.substring(0, 3);
   //check if they are equal
   if(first.equals(second)){
   System.out.println("3-char prefix matched");
   }
   else{
   System.out.println("3-char prefix didn’t match");
   }
  
   }
   public static void checkSuffix(String a, String b){
   //get the last 3 char of string
   String first = a.substring(a.length()-3, a.length());
   String second = b.substring(b.length()-3, b.length());
   //check if they are equal
   if(first.equals(second)){
   System.out.println("3-char suffix matched");
   }
   else{
   System.out.println("3-char suffix didn’t match");
   }
  
   }
}

Screenshots-

Add a comment
Know the answer?
Add Answer to:
in java!! Write a method to check prefix of two strings that have more than 3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA!!!! Write a method that returns the longest common prefix between a phrase A and...

    IN JAVA!!!! Write a method that returns the longest common prefix between a phrase A and a phrase B. If the two phrases do not share a common prefix, return the empty string “no prefix”. Your method should either take two strings or two char arrays as arguments (A, B) and return a string. Test your method in the main with these three pairings. Example A: snowball B: snowcone Return: “snow” A: river B: rover Return: “r” A: monday B:...

  • Write a program that uses String method regionMatches to compare two strings input by the user....

    Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal, (Ignore the case of the characters during comparison.) 四SAMPLE RUN #1: java StringCompare Highlight: None D Show Highlighted Only Interactive Session...

  • Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int...

    Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int col) This method does the following: compare the first character from word to the character in puzzle[row][col] if they match subtract one from row (to check the previous character in the same column) and continue comparing characters, by advancing to the next character in word else if puzzle[row][col] is NOT part of puzzle array or the character in the cell does not match the...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • Construct context-free grammars that generate the given set of strings. If the grammar has more than one variable, we wi...

    Construct context-free grammars that generate the given set of strings. If the grammar has more than one variable, we will ask to write a sentence describing what sets of strings expect each variable in the grammar to generate. For example, if the grammar was: I could say "C generates binary strings of length one, E generates (non-empty) even length binary strings, and O generates odd length binary strings." It is also fine to use a regular expression, rather than English,...

  • must be done in C Write a program that uses function strncmp to compare two strings...

    must be done in C Write a program that uses function strncmp to compare two strings input by the user. The program should input the number of characters to be compared, then display whether the first string is less than, equal to, or greater than the second string.

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • using java String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    Have the function wildcard(str) read str which will contain two strings separated by a space.The first string will consist of the following sets of characters: +, *, $ and {N} which is optional.The plus (+) character represents a single alphabetic character, the ($) character represents anumber between 1-9, and asterisk (*) represents a sequence of the same character of length 3unless it is followed by {N} which represents how many characters would appear in thesequence where N will be at...

  • In java, write a program with a recursive method which asks the user for a text...

    In java, write a program with a recursive method which asks the user for a text file (verifying that the text file exists and is readable) and opens the file and for each word in the file determines if the word only contains characters and determines if the word is alpha opposite. Now what I mean by alpha opposite is that each letter is the word is opposite from the other letter in the alphabet. For example take the word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT