Advertisement
Deblazin

lab 5 part 2

Oct 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package javalab5part2try2;
  2. import java.util.Scanner;
  3.  
  4. //John Semanduyev                                      Due October 18, 2018
  5. //CISC 1115 TR11                                                 LAB5 PART2
  6. public class Javalab5part2try2 {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner input= new Scanner (System.in);
  10.         System.out.println("The program loops until the input for the "
  11.         + "first string is of length 1.");
  12.         String s1,s2;
  13.        
  14. //set up loop
  15.         while (true) {
  16.             int count=0,start=0;
  17.             System.out.println("Enter two strings to check"
  18.                     + " if the second is a substring of the first");
  19.             s1=input.nextLine();
  20. //break if s1 length is 1
  21.             if (s1.length()==1) {
  22.                 System.out.println("Thanks for playing");
  23.                 break;
  24.             }
  25.             s2=input.nextLine();
  26. //make sure s1 is longer or equal to s2 length
  27.             if (s1.length()>=s2.length()) {
  28. //check how many subscript matches there are and keep count
  29.                 while (true) {
  30.                     int pos=s1.indexOf(s2,start);
  31.                     if (pos==-1) {
  32.                     break;
  33.                     }
  34.                     start=pos+s2.length();
  35.                     System.out.println("Match at position " + pos);
  36.                     count++;
  37.                 }
  38.             }
  39.             else {
  40.                 System.out.println("String 2 cannot be longer than string 1."
  41.                         + " Try again");
  42.             }
  43.             System.out.println("There were a total of " + count
  44.                     + " matches");
  45.         }
  46.     }
  47. }
  48. //run:
  49. //The program loops until the input for the first string is of length 1.
  50. //Enter two strings to check if the second is a substring of the first
  51. //ichbinsehrmude
  52. //sehr
  53. //Match at position 6
  54. //There were a total of 1 matches
  55. //Enter two strings to check if the second is a substring of the first
  56. //ichdenkedassichschlafbrauche
  57. //schlaf
  58. //Match at position 15
  59. //There were a total of 1 matches
  60. //Enter two strings to check if the second is a substring of the first
  61. //abcdef
  62. //mn
  63. //There were a total of 0 matches
  64. //Enter two strings to check if the second is a substring of the first
  65. //a
  66. //Thanks for playing
  67. //BUILD SUCCESSFUL (total time: 1 minute 27 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement