Hi,
This is ravi i have a question like this i have string
"UNDERSTAND" now i want to count the letters how many times
it occures.i.e from the above string the out put should be
like this U-1,N-2,D-2,E-1,R-1,S-1,T-1,A-1.
how can i achieve this
Thnaks in advance for your response ..
Answer Posted / john
public class Test {
public static void main (String a[]){
String s ="UNDERSTAND";
char rep[] = new char [s.length()];
StringBuffer rept=new StringBuffer();
boolean flag=false;
for(int i=0;i<s.length();i++){
int k=0;
for(int j=0;j<rept.length();j++){
if( s.charAt(i) == rept.charAt(j))
flag=true;
}
if(flag==false){
rept = rept.append(s.charAt(i));
}
}
System.out.println("Rept = "+rept);
for(int i=0;i<rept.length();i++){
char var= rept.charAt(i);
int counter=1;
for(int j=s.indexOf(var);j<s.lastIndexOf
(var);j++){
if(var == s.charAt(j)){
counter++;
}
}
System.out.println( " "+var+ " : "+counter);
}
}//main
}//Test
| Is This Answer Correct ? | 4 Yes | 1 No |
Post New Answer View All Answers
Explain java heap space and garbage collection?
Make a data structure and implement an algorithm to print all the files in a directory. (The root directory can have sub-directories too.)
What is subsequence of a string?
Write a regular expression to validate a password. A password must start with an alphabet and followed by alphanumeric characters; its length must be in between 8 to 20.
What is function and its uses?
What are the types of java?
What do you understand by copy constructor in java?
How many threads can java run?
Explain the difference between intermediate and terminal operations in java8?
How can you share data between two thread in Java?
Which collection is thread safe in java?
What is keyset in java?
What advantage do java's layout managers provide over traditional windowing systems?
How many bits is a boolean?
What is the byte range?