-2

I am developing a Java program that prompts a user to enter two strings and tests whether the second string is a substring of the first string.

Actual:

Enter a string s1:Welcome to Java
Enter a string s2:come
No match%  

Expected

Enter a string s1:Welcome to Java
Enter a string s2:come
matched at index 3

My attempt

import java.util.*;

public class Test2 {
    
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string s1:");
        String s1    = input.nextLine();
        System.out.print("Enter a string s2:");
        String s2    = input.nextLine();
        int index    = matched(s1, s2);
        if(index > 0)
            System.out.printf("matched at index %d",matched(s1, s2));
        else
            System.out.printf("No match");
    }

    public static int matched(String s1, String s2){
        return indexOfDifference(s1,s2);
    }

    public static int indexOfDifference(String str1, String str2) {
        if (str1 == str2) {
            return -1;
        }
        if (str1 == null || str2 == null) {
            return 0;
        }
        int i;
        for (i = 0; i < str1.length() && i < str2.length(); ++i) {
            if (str1.charAt(i) != str2.charAt(i)) {
                break;
            }
        }
        if (i < str2.length() || i < str1.length()) {
            return i;
        }
        return -1;
    }
}

How can I implement the matching algorithm?

1
  • You only check if they match starting at the beginning of the string. Commented Apr 4, 2022 at 19:09

3 Answers 3

0

The easiest way to do it is with indexOf(), which will return the index in the first string of the first character of the second string that you are searching for, and -1 if the second string isn't found in the first string.

indexOf

If you want to solve this problem by looping through the strings, here is one way to do it:

public static int indexOfDifference(String str1, String str2) {
    int indexOfDifference = -1;
    for(int i = 0; i < s1.length() - s2.length(); i++) {
        if(s1.substring(i, i + s2Length).equals(s2)){
            indexOfDifference = i;
        }
    }
    return indexOfDifference;
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can use the split method to do something like that :

    public static void main(String[] args) {
        String text = "Welcome to Java";
        String textToFind = "come";
        String[] split = text.split(textToFind);
        if (split.length == 1) {
            System.out.println("'" + textToFind + "' not found in '" + text + "'");
        } else {
            String textFirtPart = split[0];
            int index = textFirtPart.length();
            System.out.println("'"+textToFind+"' find at index '"+index+"'");
        }
    }

Apply split method on "Welcome to Java" with "come" parameter give you this array of String ["Wel"," to Java"].

The size of the first element will give you the index of your first occurence of "come" in the text "Welcome To Java"

Output :

'come' find at index '3'

Comments

0

You can just use indexOf():

String s1= "Welcome to Java";
String s2= "come";

// output 3
System.out.println( first.indexOf(sub) );

In your case

public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a string s1:");
    String s1 = input.nextLine();
    System.out.print("Enter a string s2:");
    String s2 = input.nextLine();

    if(s1.indexOf(s2) != -1)
        System.out.printf("matched at index %d",s1.indexOf(s2));
    else
        System.out.printf("No match");
}

1 Comment

While the speed difference is probably negligible, I'd store the result of String#indexOf instead of calling the method twice. Anyway, I'd recommend this answer. Don't forget to close the scanner.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.