Java
 Computer >> コンピューター >  >> プログラミング >> Java

文字列のすべてのサブセットを検索するJavaプログラム


この記事では、文字列のすべてのサブセットを見つける方法を理解します。文字列は、1つ以上の文字を含み、二重引用符(“”)で囲まれたデータ型です。文字列の一部またはサブセットはサブ文字列と呼ばれます。

以下は同じのデモンストレーションです-

入力がであると仮定します −

The string is defined as: JVM

必要な出力は

The subsets of the string are:
J
JV
JVM
V
VM
M

アルゴリズム

Step 1 - START
Step 2 - Declare namely
Step 3 - Define the values.
Step 4 - Initialize a temporary variable to increment after every iteration.
Step 5 - Iterate through the length of the string using two nested loops.
Step 6 - Find substring between a given range, and increment the temporary variable after every iteration.
Step 7 - Display the substrings using a loop.
Step 8 - Stop

例1

ここでは、「main」関数の下ですべての操作をバインドします。

public class Demo {
   public static void main(String[] args) {
      String input_string = "JVM";
      int string_length = input_string.length();
      int temp = 0;
      System.out.println("The string is defined as: " +input_string);
      String string_array[] = new String[string_length*(string_length+1)/2];
      for(int i = 0; i < string_length; i++) {
         for(int j = i; j < string_length; j++) {
            string_array[temp] = input_string.substring(i, j+1);
            temp++;
         }
      }
      System.out.println("The subsets of the string are: ");
      for(int i = 0; i < string_array.length; i++) {
         System.out.println(string_array[i]);
      }
   }
}

出力

The string is defined as: JVM
The subsets of the string are:
J
JV
JVM
V
VM
M

例2

ここでは、操作をオブジェクト指向プログラミングを示す関数にカプセル化します。

public class Demo {
   static void subsets(String input_string){
      int string_length = input_string.length();
      int temp = 0;
      String string_array[] = new String[string_length*(string_length+1)/2];
      for(int i = 0; i < string_length; i++) {
         for(int j = i; j < string_length; j++) {
            string_array[temp] = input_string.substring(i, j+1);
            temp++;
         }
      }
      System.out.println("The subsets of the string are: ");
      for(int i = 0; i < string_array.length; i++) {
         System.out.println(string_array[i]);
      }
   }
   public static void main(String[] args) {
      String input_string = "JVM";
      System.out.println("The string is defined as: " +input_string);
      subsets(input_string);
   }
}

出力

The string is defined as: JVM
The subsets of the string are:
J
JV
JVM
V
VM
M

  1. 台形の領域を見つけるJavaプログラム

    この記事では、台形の領域を見つける方法を理解します。台形は、少なくとも1対の辺が互いに平行な四辺形の一種です。台形の平行な側面はベースと呼ばれ、台形の非平行な側面は脚と呼ばれます。台形とも呼ばれます。 台形の面積は、式-を使用して計算されます。 (height/2 * (side_1 + side_2). i.e. Area = ½ x (sum of the lengths of the parallel sides) x perpendicular distance between parallel sides 以下は同じもののデモンストレーションです。平行な辺aとbの長

  2. 長方形の周囲を見つけるJavaプログラム

    この記事では、長方形の周囲を見つける方法を理解します。長方形の周囲長は、長方形のすべての辺の長さを加算して計算されます。 以下は長方形のデモンストレーションです。長方形の周囲は、長方形の2つの長さと2つの幅の全長です- 入力 入力が-であると仮定します The length of the sides of a rectangle are : 5, 8, 5, 8 出力 必要な出力は-になります Perimeter : 26 アルゴリズム Step 1 – START Step 2 – Declare 5 floating point variabl