Question
Divide a sentence into ‘x’ equal parts in the given sentence. If not divisible in equal parts, show an error.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class divideSentenceInXEqualParts
{
public static void main(String[] args)
{
String sen="",part="";
char chars=' ';
int i=0,len=0,temp=0,x=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
sen = sc.nextLine();
System.out.println("Enter 'x':");
x = sc.nextInt();
sen=sen.replaceAll(" ","");
len=sen.length();
if(len % x != 0)
{
System.out.println("Sentence cannot be divided into "+ x +" equal parts.");
}
else
{
System.out.println(x+" Equal parts of given sentence");
for(i = 0; i < len; i = i+(len/x))
{
/*Dividing string in x equal part */
part = sen.substring(i, i+len/x);
System.out.println(part);
}
}
}
}
Coding Store
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale
