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 ..

Answers were Sorted based on User's Feedback



Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want t..

Answer / 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

Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want t..

Answer / 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

Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want t..

Answer / 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

Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want t..

Answer / dilip

Actually what i think is take integers from a-z and
initialize each with 0



then take the string & using charAt & equalsIgnoreCase
increment the respaective values & then print the same.....

Is This Answer Correct ?    0 Yes 1 No

Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want t..

Answer / vignesh1988i

actually i cant understand your problem correctly..... wat
actually are you trying to say sir?????????? pl. reply...

Is This Answer Correct ?    0 Yes 3 No

Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want t..

Answer / pal_ss

public class Test {

public static void main(String args[]) {
String s = "UNDERSTAND";
System.out.println("String " + s);
for (int i = 0; i < s.length(); i++) {
int counter = 1;
for (int j = i + 1; j < s.length(); j++) {

if (s.charAt(i) == s.charAt(j)) {
counter++;
}

}
System.out.println(s.charAt(i) + "-" + counter);
}

}

}

Is This Answer Correct ?    5 Yes 9 No

Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want t..

Answer / kanthi

well, one way of doing this is to take up a counter
variable initialised to 0. Then, there will be 2 for loops.
First one starts at 0 and the second one starts at 1+(the
upper loop variable) and both iterate for a
UNDERSTAND.length() . In the loop, u can start by comparing
each char in the string with every other char using charAt
() method. If a match is found then increment the counter
variable. At the end of the inner loop, u will have the
number of times a particular character is repeated. Just
print the result out. That way, counter variable can be
reused for the next character again.

Code might be something like this:
int counter = 0;
for(int i=0;i<str.length();i++){
for(int j=i+1;j<str.length();j++)
{
if(charAt(i).equals(charAt(j))
counter++;
}
System.out.println(charAt(i) + '-' + counter);
}

Hope this works.. please tell me if u find any mistake with
the logic.

Is This Answer Correct ?    1 Yes 6 No

Post New Answer

More Core Java Interview Questions

Is empty set an element of empty set?

0 Answers  


What are the characteristics provided in jdk1.6 apart from other versions?

3 Answers   TCS,


What is the use of put method?

0 Answers  


What is synchronization and why is it important in java programming?

0 Answers  


Does apple use java?

0 Answers  






33. try { 34. // some code here 35. } catch (NullPointerException e1) { 36. System.out.print(”a”); 37. } catch (RuntimeException e2) { 38. System.out.print(”b”); 39. } finally { 40. System.out.print(”c”); 41. } What is the result if a NullPointerException occurs on line 34? 1 c 2 a 3 ab 4 ac

5 Answers  


What is the need of "creating and throwing an UserdefinedException" when the "Exception" class is already available?

4 Answers  


What is the difference between dom and sax parser in java?

0 Answers  


What is incompatible types in java?

0 Answers  


What is the difference between instanceof and isinstance?

0 Answers  


What is immutable data?

0 Answers  


What is http client in java?

0 Answers  


Categories