Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để chia một chuỗi thành 'N' các phần bằng nhau

Trong bài này, chúng ta sẽ hiểu cách chia một chuỗi thành 'N' phần bằng nhau. Chuỗi là một kiểu dữ liệu chứa một hoặc nhiều ký tự và được đặt trong dấu ngoặc kép (“”).

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

Input string: Java Program is fun!

Đầu ra mong muốn sẽ là -

The length of the string is: 20
4 equal parts of given string are
Java
Progr
am is
fun!

Thuật toán

Step 1 - START
Step 2 - Declare a string namely input_string, two integers namely string_length and N.
Step 3 - Define the values.
Step 4 - Initiatize a temporary variable to 0.
Step 5 - Compute the parts in the string by dividing the length of the string by ‘N’.
Step 6 - If the string is not divisible by N, display a relevant message. Given that the string length is divisible by N, iterate through the string.
Step 7 - Fetch the substring within the range of string length and N in every iteration. Assign this value to a variable.
Step 8 - Increment the temporary variable after every iteration.
Step 9 - Display the N parts of the string on the console by iterating over the split parts of the string.
Step 10 - Stop

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "main".

public class Demo {
   public static void main(String[] args) {
      String input_string = "Java Program is fun!";
      System.out.println("The string is defined as: " +input_string);
      int string_length = input_string.length();
      System.out.println("The length of the string is: " +string_length);
      int N = 4;
      int temp = 0, string_parts = string_length/N;
      String[] equalStr = new String [N];
      if(string_length % N != 0) {
      System.out.println("The string cannot be divided int "+ N +" parts.");
      } else {
         for(int i = 0; i < string_length; i = i+string_parts) {
            String part = input_string.substring(i, i+string_parts);
            equalStr[temp] = part;
            temp++;
         }
         System.out.println(N + " equal parts of given string are ");
         for(int i = 0; i < equalStr.length; i++) {
            System.out.println(equalStr[i]);
         }
      }
   }
}

Đầu ra

The string is defined as: Java Program is fun!
The length of the string is: 20
4 equal parts of given string are
Java
Progr
am is
fun!

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm thể hiện lập trình hướng đối tượng.

public class Demo {
   static void divide_string(String input_string, int N){
      int string_length = input_string.length();
      System.out.println("The length of the string is: " +string_length);
      int temp = 0, string_parts = string_length/N;
      String[] equalStr = new String [N];
      if(string_length % N != 0) {
      System.out.println("The string cannot be divided int "+ N +" parts.");
      } else {
         for(int i = 0; i < string_length; i = i+string_parts) {
            String part = input_string.substring(i, i+string_parts);
            equalStr[temp] = part;
            temp++;
         }
         System.out.println(N + " equal parts of given string are ");
         for(int i = 0; i < equalStr.length; i++) {
            System.out.println(equalStr[i]);
         }
      }
   }
   public static void main(String[] args) {
      String input_string = "Java Program is fun!";
      System.out.println("The string is defined as: " +input_string);
      int N = 4;
      divide_string(input_string, N);
   }
}

Đầu ra

The string is defined as: Java Program is fun!
The length of the string is: 20
4 equal parts of given string are
Java
Progr
am is
fun!