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 / pradeep mishra

public class CountOccur {
String str="ANAND";
int c1=0;
int c2=0;
int c3=0;
String retun=null;

public static void main(String[] args) {
CountOccur co=new CountOccur();
System.out.println("Ocurrence "+co.Test());
}

public String Test() {
for( int i=0;i<str.length();i++){
if(str.charAt(i)=='A')
c1++;
if(str.charAt(i)=='N')
c2++;
if(str.charAt(i)=='D')
c3++;
}
retun="A"+c1+"N"+c2+"D"+c3;
return retun;
}
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

I want to re-reach and use an object once it has been garbage collected. How it's possible?

542


Can we have 2 main methods in java class?

543


Why char array is preferred over string for storing password?

590


What is hash in java?

523


Explain the Propertie sof class?

586






What's a method in programming?

551


Why main function is static?

674


Define locale.

588


What are the three best choices for a development environment?

609


What is difference between public static and void?

486


What is the difference between abstraction and encapsulation?

562


Explain the importance of finally over return statement?

610


Difference between keyword and identifier.

595


Why we use methods in java?

541


What are the 6 boolean operators?

553