+ 2
can anyone help me to explain on how to solve this question (SortChar)
Write a java class with name SortChar to do the character sorting base on the character input and also display the character numbering. For example Input: APPLE Output result: AELPP A=1 (1) E=5 (1) Five is the location of the char, (One is total char count). L=12 (1) P=16 (2) • System should able to support up to 20 characters. • Support small and capital letter • Not need to support symbols and system require doing the validation.
3 Antworten
+ 3
import java.util.ArrayList;
class SortChar {
public static void main(String[] args) {
SortChar sc = new SortChar();
int arr[]=sc.sort("ap8p6le");
sc.print(arr);
}
public int[] sort(String str){
int[] arr = new int[26];
str = str.toUpperCase();
for(int i=0; i<str.length(); i++){
if((str.charAt(i)>='A' && str.charAt(i)<='Z') || (str.charAt(i)>='a' && str.charAt(i)<='z')){
int indx=str.charAt(i)-'A';
arr[indx]++;
}
}
return arr;
}
public void print(int[] arr){
for(int i=0;i<26;i++){
if(arr[i] > 0)
System.out.println((char)(i+'A') + "("+ (i+1) + ")"+":" +arr[i]);
}
}
}
note that every input will result in only upper case letters
+ 3
my pleasure :)
0
Thank you very much