Question

In: Computer Science

Write a JAVAprogram that reads a word and prints all its substrings, sorted by length. It...

Write a JAVAprogram that reads a word and prints all its substrings, sorted by length. It then generates a random index between 0 and the length of the entered word, and prints out the character at that index.

For example, if the user provides the input "rum", the program prints
r
u
m
ru
um
rum

The random index generated is 1. The character at index 2 is u.

Solutions

Expert Solution

import java.util.Scanner;
import java.util.Random;

public class Main
{
   public static void main(String[] args)
   {
   //variable declaration
   String s;
   int n, randNum;
       Scanner sc = new Scanner(System.in);
       //display message
        System.out.print("Enter a string: ");
        //input string
        s = sc.nextLine();
        //calculate the length of the input string
        n = s.length();
       
        //generate random number
        Random rand = new Random();
randNum = rand.nextInt(n);
       
        //display message
        System.out.println("The substring are: ");
       
        //generate all substring
        for (int len = 1; len <= n; len++)
        {
for (int i = 0; i <= n - len; i++)
{
int j = i + len - 1;
for (int k = i; k <= j; k++)
{
System.out.print(s.substring(k, k+1));
}
  
System.out.println();
}
}
//display character at random position
System.out.println("\nThe random number generated is " + randNum + ". The character at index " + randNum + " is " + s.charAt(randNum-1));
   }
}

OUTPUT:


Related Solutions

Write an application, CylinderStats, that reads the radius and height of a cylinder and prints its...
Write an application, CylinderStats, that reads the radius and height of a cylinder and prints its surface area and volume. Use the following formulas. Print the output to five decimal places. In the formulas, represents the radius and the height.Surface are 2πr(r+ h) Volume:πr2h Design and implement a class Cylinder that contains instance data that represents the cylinder’s radius and height. Define a Cylinder constructor to accept and initialize the radius and height.Include getter and setter methods for all instance...
Write a program that reads a positive integer n , prints all sums from 1 to...
Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers from 1 to 4 is 10;      ...
Write a program that reads and prints a joke and its punch line from two different...
Write a program that reads and prints a joke and its punch line from two different files. The first file contains a joke, but not its punch line. The second file has the punch line as its last line, preceded by “garbage.” The main function of your program should open the two files then call two functions, passing each one the file it needs. The first function should read and display each line in the file it is passed (the...
Design and implement an application that reads an integer value and prints the sum of all...
Design and implement an application that reads an integer value and prints the sum of all even integers between 2 and the input value, inclusive. Print an error message if the input value is less than 2 and prompt accordingly so that the user can enter the right number. Your program file will be called YourLastNameExamQ2
Write a Python program that reads in an amount in cents and prints the dollar amount...
Write a Python program that reads in an amount in cents and prints the dollar amount in that and the remaining value in cents. For example, if the amount reads in is 360 (cents), the program would print 3 dollars and 60 cents. if the amount read in is 75 (cents), the program would print 0 dollars and 75 cents.
in.java Write a program that reads an integer from the user and prints a rectangle of...
in.java Write a program that reads an integer from the user and prints a rectangle of starts of width 5 3 and height N. Sample run 1: Enter N: 5 *** *** *** *** *** Bye Sample run 2: Enter N: 8 *** *** *** *** *** *** *** *** Bye Sample run 3: Enter N: 2 *** *** Bye Sample run 4: Enter N: -2 Bye
Write a Fortran program that reads in two NxN matrices A & B, and prints their...
Write a Fortran program that reads in two NxN matrices A & B, and prints their element-wise sum (A+B), element-wise difference (A-B), element-wise division (A/B), element-wise product (A*B), and their matrix product (matmul(A,B)), on the standard output.
Write a program that asks the user for an integer and then prints out all its...
Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop.in c++
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints...
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints the decimal value of the binary number represented by the entered bits, i.e. 25 in this case.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT