Advertisement
Guest User

tt2

a guest
Apr 25th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. public class cl {
  4.  
  5.     public static void main(String[] args) {
  6.         while(true){
  7.             Scanner sc = new Scanner(System.in);
  8.             String str1,str2;
  9.             boolean stringfound = true;
  10.             System.out.print("Enter input string: ");
  11.             str1 = sc.next();
  12.             System.out.print("Enter search string: ");
  13.             str2 = sc.next();
  14.             while(str2.length() > 1 && str2.charAt(1) == '*')
  15.             {
  16.                 if(str2.length() > 2)
  17.                     str2 = str2.substring(2);
  18.                 else
  19.                 {
  20.                     str2 = "";
  21.                     System.out.println("Search string matches input string."); 
  22.                 }
  23.             }
  24.  
  25.             /////////////////////////////////////////////////////////////////////////
  26.             //                   input validity test                               //
  27.             ////////////////////////////////////////////////////////////////////////
  28.             if(str1.length() > 0 && str2.length() > 0)
  29.             {
  30.                 if(str2.charAt(0) != '+' && str2.charAt(0) != '*')
  31.                 {
  32.                     boolean validstr = true;
  33.                     //Test for repeating signs
  34.                     for(int y = 0; y < str2.length()- 1; y++)
  35.                     {
  36.                         if((str2.charAt(y) == '*' || str2.charAt(y) == '+') &&
  37.                                 (str2.charAt(y+1) == '*' || str2.charAt(y+1) == '+'))
  38.                         {
  39.                             validstr = false;
  40.                         }
  41.                     }
  42.                     if(validstr)
  43.                     {
  44.                         for(int i = str1.length() - 1; i > 0 ; i--)
  45.                         {
  46.                             /////////////////////////////////////////////////////////////////////////
  47.                             //                   suspect substring test                            //
  48.                             ////////////////////////////////////////////////////////////////////////
  49.                                 stringfound = true;
  50.                                 for(int j = str2.length() - 1; j >= 0 && stringfound ; j--)
  51.                                 {
  52.                                     if(str2.charAt(j) == str1.charAt(i))
  53.                                     {
  54.                                         i--;
  55.                                     }
  56.                                     else
  57.                                     {
  58.                                         if(str2.charAt(j) == '*')
  59.                                         {
  60.                                             if(j == str2.length() - 1)
  61.                                             {
  62.                                                 while(i > 0 && str1.charAt(i) != str2.charAt(j-1))
  63.                                                     i--;
  64.                                                 if(i == 0 && str1.charAt(i) != str2.charAt(j-1))
  65.                                                     i = str1.length() - 1;
  66.                                             }
  67.                                             while(i > 0 && str1.charAt(i) == str2.charAt(j-1))
  68.                                             {
  69.                                                 i--;
  70.                                             }
  71.                                             j--;
  72.                                         }
  73.                                         else if(str2.charAt(j) == '+')
  74.                                         {
  75.                                             if(j == str2.length() - 1)
  76.                                             {
  77.                                                 while(i > 0 && str1.charAt(i) != str2.charAt(j-1))
  78.                                                     i--;
  79.                                             }
  80.                                             if(str1.charAt(i) != str2.charAt(j-1))
  81.                                                 stringfound = false;
  82.                                             while(i > 0 && str1.charAt(i) == str2.charAt(j-1))
  83.                                             {
  84.                                                 i--;
  85.                                             }
  86.                                             j--;
  87.                                         }
  88.                                         else if(j == str2.length()-1)
  89.                                         {
  90.                                             while(i > 0 && str1.charAt(i)!= str2.charAt(j))
  91.                                                 i--;
  92.                                             if(i == 0 && str1.charAt(i)!= str2.charAt(j))
  93.                                                 stringfound = false;
  94.                                             j++;
  95.                                         }
  96.                                         else
  97.                                         {
  98.                                             stringfound = false;
  99.                                         }
  100.                                     }
  101.                                 }
  102.                                 if(stringfound)
  103.                                 {
  104.                                     System.out.println("Search string matches input string.");
  105.                                     break;
  106.                                 }
  107.                         }
  108.                     }
  109.                     if(!stringfound)
  110.                         System.out.println("Search string doesn't match input string.");
  111.                 }
  112.                 else
  113.                 {
  114.                     System.out.println("Invalid search string.");
  115.                 }
  116.  
  117.             }
  118.             else if(str2.length() != 0)
  119.             {
  120.                 System.out.println("Invalid search string.");
  121.             }
  122.             /////////////////////////////////////////////////////////////////////////
  123.             //End                    input validity test                        End//
  124.             ////////////////////////////////////////////////////////////////////////
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement