Javas string class provides the following nonstatic methods

Java\'s string class provides the following non-static methods: string to UpperCase () returns a string that contains all the characters of the calling string, converted to uppercase. char charAt(int i) returns the character located at index i of the calling string. int length () returns the total number of characters in the calling string. string substring (int i, int j) returns the substring consisting of the characters between indices i and j-1 (inclusive) of the calling string. For example, if the string str has the value \"waffles\", then calling str. substring (1, 4) would return the string \"aff\". Calling str. substring (5, str. length ()) would return the string \"es\". Using these methods, write a method public static string jumble (String s) that returns a random permutation (rearrangement of characters) of the parameter s. The returned string should be in uppercase (regardless of the case of s). For example, calling jumble (\"Holst\") could return \"SLOTH\" or \"HOLTS\", among many other possibilities. Your method should jumble the letters by repeatedly swapping the first character of the string with the character at a random index. This process should be repeated for a number of times that is equal to the length of s. For example, calling jumble (\"Holst\") should carry out 5 swaps, while calling jumble (\"Mendelssohn\") should carry out 11 swaps.

Solution

package org.students;

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

public class JumbleLetters {

   public static void main(String[] args) {
       String word;
      
       // Scanner object is used to get the inputs entered by the user
       Scanner sc = new Scanner(System.in);
      
       //Getting the word entered by the user
       System.out.print(\"Enter a word :\");
       word = sc.next();

       //calling the method by passing user entered input as argument
       String jumbleStr = jumble(word);
      
       //Displaying the output
       System.out.println(\"The Output is :\" + jumbleStr);

   }

   //This method will jumble the word and convert it into Upper case and return
   private static String jumble(String str) {
      
       //Declaring the variables
       char temp;
       int index;
       String jumbleStr = \"\";

       //Creating the random class obejct
       Random rand = new Random();
      
       //getting the random number
       index = rand.nextInt(str.length());
      
       //This loop will jumble the word
       while (str.length() != 0) {
           index = rand.nextInt(str.length());
           temp = str.charAt(index);
           str = str.substring(0, index) + str.substring(index + 1);
           jumbleStr += temp;
       }
       return jumbleStr.toUpperCase();
   }

}

__________________

output:

Enter a word :Holst
The Output is :LHOTS

_________Thank You

 Java\'s string class provides the following non-static methods: string to UpperCase () returns a string that contains all the characters of the calling string,
 Java\'s string class provides the following non-static methods: string to UpperCase () returns a string that contains all the characters of the calling string,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site