Java Program to divide a string in 'N' equal parts.8 Jan 2025 | 2 min read Here, our task is to divide the string S into n equal parts. We will print an error message if the string cannot be divisible into n equal parts otherwise all the parts need to be printed as the output of the program. To check whether the string can be divided into N equal parts, we need to divide the length of the string by n and assign the result to variable chars. If the char comes out to be a floating point value, we can't divide the string otherwise run for loop to traverse the string and divide the string at every chars interval. The algorithm of the program is given below. Algorithm
Program:Output: 3 equal parts of given string are aaaa bbbb cccc Next TopicJava Programs |
Java Convert String to boolean We can convert String to boolean in java using Boolean.parseBoolean(string) method. To convert String into Boolean object, we can use Boolean.valueOf(string) method which returns instance of Boolean class. To get boolean true, string must contain "true". Here, case is ignored. So, "true" or "TRUE"...
1 min read
Java program to print the following pattern on the console 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Algorithm: STEP 1: START STEP 2: DEFINE i, j. STEP 3: SET n=5. STEP 4: SET i=n. REPEAT STEP 5 to STEP 7 UNTIL i>0. STEP 5: SET j=1. REPEAT STEP...
2 min read
Java Program to Print Spiral Pattern The spiral pattern (or matrix in spiral form) is frequently asked in Java interviews and academics. In this section, we will create a Java program to create a spiral pattern or spiral matrix. What is Spiral Matrix or Spiral Pattern? A spiral...
7 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, HCL, IBM, and Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are...
4 min read
Java long to String We can convert long to String in java using String.valueOf() and Long.toString() methods. Scenario It is generally used if we have to display long number in textfield in GUI application because everything is displayed as a string in form. 1) String.valueOf() The String.valueOf() is an overloaded method....
1 min read
Write a code to prove that strings are immutable in java? Following program demonstrate it. File: ProveStringImmutable .java public class ProveStringImmutable { public static void referenceCheck(Object x, Object y) { if (x == y) { System.out.println("Both pointing to the same reference"); } else { System.out.println("Both are pointing to different reference"); } } public static void...
1 min read
Problem Statement The problem states that a gardener wants to water a garden (single-dimensional) by opening the minimum number of taps. The task is to find the minimum number of taps that should be open to water the whole garden, return -1 if the garden cannot be...
17 min read
In this program, we need to count and print the number of elements present in the array. The number of elements present in the array can be found by calculating the length of the array. Length of above array is 5. Hence, the number of elements present...
1 min read
Java Convert float to String We can convert float to String in java using String.valueOf() and Float.toString() methods. Scenario It is generally used if we have to display float value in textfield because everything is displayed as a string in form. 1) String.valueOf() The String.valueOf() is an overloaded method. It can...
1 min read
In this program, we need to search a node in the given singly linked list. Identity Matrix To solve this problem, we will traverse through the list using a node current. Current points to head and start comparing searched node data with current node data. If they...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India