Write a java program to find a substring in string

Given three strings say Searchstring, Str1 and Str2 as input, write a program to find out if Str2 comes after Str1 in the Searchstring.

The 3 arguments correspond to SearchString, Str1 and Str2. The function returns 1 if Str2 appears after Str1 in the Searchtring. Else it returns 2.

Input and Output Format:

Input consists of 3 strings.

The first input corresponds to the SearchString.

The second input corresponds to Str1.

The third input corresponds to Str2.

Output consists of a string that is either “yes” or “no”

Sample Input 1:

geniousRajKumarDev

Raj

Dev

Sample Output 1:

yes

Sample Input 2:

geniousRajKumarDev

Dev

Raj
Sample Output 2:

No

Find a substring in the string in Java.

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
    String s2 = sc.nextLine();
    String s3 = sc.nextLine();

    getvalues(s1, s2, s3);
  }

  public static void getvalues(String s1, String s2, String s3) {
    if (s1.contains(s2) && s1.contains(s3)) {
      if (s1.indexOf(s2) < s1.indexOf(s3)) {
        System.out.println("YES");
      } else
        System.out.println("No");
    }
  }
}

You May Also Like These Posts:

Leave a Reply

Your email address will not be published.

twenty − 13 =