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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
what is meant by multicast?
what is actual real time using of oops concepts in projects(Interface,polymorphism.abstraction.........)
Is logger a singleton?
What's the base class in java from which all classes are derived?
What are different types of Exceptions?.
Which method returns the length of a string?
What is difference between final and immutable?
What is boolean false?
What is the simpletimezone class in java programming?
String is a immutable objects . it means that string does not change........... But it will be chang......... { String s="kapil"; String s1="raj"; String s=s1; then print(.......) The String has been changed .. how it is possible and why its called immutable objects
Why is it called boolean?
What is starvation?