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 / manikandansit
import java.util.Vector;
class test
{
public static void main(String []dasf)
{
String s="Manikandan,GTEC,vellore" ;
int count,a,c;
for(int i=0;i<s.length();i++)
{
count=0;c=0;a=0;
for(int j=0;j<s.length();j++) //counting
{
if(s.charAt(i)==(s.charAt(j)))
{
count++;
}
} while(a<=i)// checking duplicate
{
if(s.charAt(i)==s.charAt(a))
{
c++;
}
a++;
}
if(c==1)
System.out.println(s.charAt(i)+" = "+count);
}
}
}
o/p:
M-1
a-3
n-3
i-1
k-1
d-1
,-2
G-1
T-1
E-1
C-1
v-1
e-2
l-2
o-1
r-1
i done it for case sensitive.
In the checking Duplicate code "c" will increment more than
one if duplicate presents.
if any doubt in above code contact me at
manikandansit@gmail.com
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
what is synchronization? : Java thread
How can I debug the Java security exceptions and AccessControlExceptions?
If we don’t want some of the fields not to serialize how to do that?
Why is string builder not thread safe?
Explain about class in java?
Compare overloading and overriding?
Is string is a data type?
Suppose if we have variable ' I ' in run method, if I can create one or more thread each thread will occupy a separate copy or same variable will be shared?
How do you read and print a string in java?
What happens when I use / and % with a negative numerator?
Is it possible to compare various strings with the help of == operator? What are the risks involved?
Explain the inheritance?
Is it possible to override private or static method in java?
What is treeset in java?
What is a java list?