इस लेख में, हम समझेंगे कि किसी स्ट्रिंग को 'N' बराबर भागों में कैसे विभाजित किया जाए। स्ट्रिंग एक डेटाटाइप है जिसमें एक या अधिक वर्ण होते हैं और दोहरे उद्धरण चिह्नों ("") में संलग्न होते हैं।
नीचे उसी का एक प्रदर्शन है -
मान लीजिए कि हमारा इनपुट है -
Input string: Java Program is fun!
वांछित आउटपुट होगा -
The length of the string is: 20 4 equal parts of given string are Java Progr am is fun!
एल्गोरिदम
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
उदाहरण 1
यहां, हम 'मेन' फंक्शन के तहत सभी ऑपरेशंस को एक साथ बांधते हैं।
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]); } } } }
आउटपुट
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!
उदाहरण 2
यहां, हम ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग को प्रदर्शित करने वाले कार्यों में संचालन को समाहित करते हैं।
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); } }
आउटपुट
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!