Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javalab5part2try2;
- import java.util.Scanner;
- //John Semanduyev Due October 18, 2018
- //CISC 1115 TR11 LAB5 PART2
- public class Javalab5part2try2 {
- public static void main(String[] args) {
- Scanner input= new Scanner (System.in);
- System.out.println("The program loops until the input for the "
- + "first string is of length 1.");
- String s1,s2;
- //set up loop
- while (true) {
- int count=0,start=0;
- System.out.println("Enter two strings to check"
- + " if the second is a substring of the first");
- s1=input.nextLine();
- //break if s1 length is 1
- if (s1.length()==1) {
- System.out.println("Thanks for playing");
- break;
- }
- s2=input.nextLine();
- //make sure s1 is longer or equal to s2 length
- if (s1.length()>=s2.length()) {
- //check how many subscript matches there are and keep count
- while (true) {
- int pos=s1.indexOf(s2,start);
- if (pos==-1) {
- break;
- }
- start=pos+s2.length();
- System.out.println("Match at position " + pos);
- count++;
- }
- }
- else {
- System.out.println("String 2 cannot be longer than string 1."
- + " Try again");
- }
- System.out.println("There were a total of " + count
- + " matches");
- }
- }
- }
- //run:
- //The program loops until the input for the first string is of length 1.
- //Enter two strings to check if the second is a substring of the first
- //ichbinsehrmude
- //sehr
- //Match at position 6
- //There were a total of 1 matches
- //Enter two strings to check if the second is a substring of the first
- //ichdenkedassichschlafbrauche
- //schlaf
- //Match at position 15
- //There were a total of 1 matches
- //Enter two strings to check if the second is a substring of the first
- //abcdef
- //mn
- //There were a total of 0 matches
- //Enter two strings to check if the second is a substring of the first
- //a
- //Thanks for playing
- //BUILD SUCCESSFUL (total time: 1 minute 27 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement