Friday, 29 June 2012

Checking Subset String In Java

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

Internet-PublicLab. Powered by Blogger.