Question

In: Computer Science

Write a java method that takes a string and returns an array of int that contains...

Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string:

An illustration:

the method takes: "Sara"

the method returns: {4,1,3,2}

another illustration:

the method takes: "hey"

the method returns: {2,1,3}

Solutions

Expert Solution

Code:

import java.util.*;

public class alphabetOrder

{

static int[] findOrder(String str)

{

// Storing the size of the string

int strSize = str.length();

// This value is used to keep track of order of the character

int value = 0;

// Declaring an array whose size is same as the size of the string

int order[] = new int[strSize];

// Iterating over ASCII values of lowercase alphabets

for (int i = 97; i <= 122; i++)

{

// Iterating over the string

for (int j = 0; j < strSize; j++)

{

// If the ASCII value of character at index j in the string str is equal to i

if ((int) str.charAt(j) == i)

{

// Increment the value by 1

value++;

// Storing the value at index j in the array

order[j] = value;

}

}

}

// Returning the order array

return order;

}

public static void main(String[] args)

{

// Creating an scanner object

Scanner inp = new Scanner(System.in);

// Prompting the user to enter the string

System.out.print("Enter the string: ");

// Reading the string from the user

String str = inp.nextLine().toLowerCase();

// Storing the int array returned by the findOrder method

int result[] = findOrder(str);

// Printing the result array

System.out.println(Arrays.toString(result));

// Closing the scanner object

inp.close();

}

}

Sample Output:


Related Solutions

Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array of single        words which are then are edited based on the command        Possible Commands:        remove: for the sent words, remove those from the text        replace: replace the word in an even element of words with a word in an odd element of words        add: add the word given word in the index indicated after...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT