Checking Subset String In Java

[postlink]https://iamdaowner.blogspot.com/2012/06/checking-subset-string-in-java.html[/postlink]
This program used Scanner method for getting user's input. Save this file as SubSet.java.

01import java.util.Scanner;
02
03public class SubSet {
04 public static void main(String[] args) {
05     Scanner in = new Scanner(System.in);
06     String superString, subString;
07     boolean occur = false;
08
09     System.out.print("Enter a string for superString: ");
10  superString = in.nextLine();
11
12  System.out.print("Enter a string for subString: ");
13  subString = in.nextLine();
14
15  for(int i = 0; i < subString.length(); i++) {
16   for(int j = 0; j < superString.length(); j++) {
17    if(subString.charAt(i) == superString.charAt(j)) {
18     occur = true;
19     break;
20    }
21    else {
22     occur = false;
23    }
24   }
25
26   if(!occur) {
27     break;
28   }
29  }
30
31  if(occur) {
32   System.out.print("All characters of subString occur in superString");
33  }
34  else {
35   System.out.print("Not all characters of subString occur in superString");
36  }
37 }
38}

0 comments:

Post a Comment

Grab the widget  IWeb Gator
Powered by Blogger.

Click the Like Button Below To Receive all updates via Facebook

Powered By Blogger Tricks |

Popular Posts