Home > database >  How to write a program that finds the matching index of two strings in Java
How to write a program that finds the matching index of two strings in Java

Time:04-05

I 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;
    }
}

Any help figuring out how to implement the matching algorithm would be greatly appreciated. Thank you!

CodePudding user response:

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'

CodePudding user response:

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");
}

CodePudding user response:

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;
}
  •  Tags:  
  • java
  • Related