Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Number of Occurrences of characters in a string

Fri Oct 24, 2008 8:38 pm

Count the number of characters in a string, i have one simple problem.iam not getting it right.
the problem is....
the user enters one string.our program has to read the string entered by
user and then display the no.of occurances of individual characters in that
string.

for example if user enters a sring "character" then the output should be in
ascending order like...
a 2
c 2
e 1
h 1
r 1
t 1



Re: Number of Occurances of characters in a string

Fri Oct 24, 2008 8:39 pm

i found the solution:
Code:
class SampleTest
{
public static void main(String arg[])
{
String str="hhaahhhr";
char[] c=str.toCharArray();
int loopcount=0;
int count=0;

for(int i=0;i<c.length;i++)
{
//System.out.println(c[i]+" "+i);
boolean flag=true;
for(int k=0;k<i;k++){
if(c[i]==(str.charAt(k)))
flag=false;
}
if(flag){
for(int j=0;j<str.length();j++)
{
if(c[i]==str.charAt(j))
count=count+1;
}
System.out.println(c[i]+" "+" "+(count));
count=0;
loopcount++;
}
}
}
}


Post a reply
  Related Posts  to : Number of Occurrences of characters in a string
 Find occurrences of each character in a string     -  
 continuously calculates the number of characters existing     -  
 How to remove special characters from a string in Java?     -  
 get number from string     -  
 get number of words in a string     -  
 convert integer number to octal,hexadecimal number systems     -  
 convert octal number to decimal number     -  
 convert decimal number to octal number     -  
 URL Characters and URL Encoding Values     -  
 Special Characters literals     -  

Topic Tags

Java Strings