Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

What is printwriter in java?

0 Answers  


Is ruby built on java?

0 Answers  


How GC (Garbage Collector) knows the objects reference is unused.Whether GC removes the unused object Parmanently or it maintains something.

2 Answers  


Is arraylist ordered?

0 Answers  


Why pass by reference is not possible in java?

0 Answers  


I need some details about an employee. i have only the employee id in my presentation layer(JSP).I entered that ID and click "Show Details" Button. Question is how the JSP pass that id to Controller layer and DAO and what mechanism we are using to retrive data from DB and how the data passed to JSP as a Output. Please explain in detail.

1 Answers   TCS,


What is unmodifiable list in java?

0 Answers  


Explain Big-O notation with an example

1 Answers  


What is a treeset class?

0 Answers  


what is main difference between architecture,framework and design pattren

3 Answers  


what is for datainputstream?

1 Answers  


What is difference between adapter class and listener?

0 Answers  


Categories