tampurus

27 Search string within another string

Apr 26th, 2022 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. // Q27 Search string within another string
  2. import java.util.*;
  3. public class Main
  4. {
  5.     public static void main(String[] args) {
  6.         Scanner sc= new Scanner(System.in);
  7.         System.out.println("Enter the exhaustive string");
  8.         String s1 =sc.nextLine();
  9.         System.out.println("Enter the string you want to search");
  10.         String s2 =sc.nextLine();
  11.         int j=0;
  12.         for(int i=0 ; i<s1.length() ; i++){
  13.             if(s1.charAt(i)==s2.charAt(0)){
  14.                 for(int j=0 ; j<s2.length() ; j++ ){
  15.                     if(s1.charAt(i)!=s2.charAt(j)){
  16.                         check = false;
  17.                         break;
  18.                     }
  19.                     i++;
  20.                 }
  21.                 break;
  22.             }
  23.             if(s1.charAt(i)==s2.charAt(j)){
  24.                 j++;
  25.             }
  26.         }
  27.         if(j>=s2.length()){
  28.             System.out.println("First string does not contain second string");
  29.         }
  30.         else{
  31.             System.out.println("First string contain second string");
  32.         }
  33.     }
  34. }
Add Comment
Please, Sign In to add comment