Total members 11816 |It is currently Tue Feb 18, 2020 2:48 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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




Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

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++;
}
}
}
}


_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  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 decimal number to octal number     -  
 convert octal number to decimal number     -  
 URL Characters and URL Encoding Values     -  
 Special Characters literals     -  



Topic Tags

Java Strings






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com