Write program to print Hello World and print each character
address in that string and print how many times each
character is in that string?
Ex: H: 0 & 1
e:1 & 1
l :2,3,8 & 3
o:4,6 & 2
w:5 & 1
r: 7 & 1
d 9 & 1

Answer Posted / kishore nerella

class Wordcount
{
public static void main(String[] args)
{

String s="hello world";

for(int i=0;i<s.length();i++)
{
String s1=""+i;

int count=1;
if(s.charAt(i)!=' ')
{

for(int j=i+1;j<s.length();j++)
{
if(s.charAt(i)==s.charAt(j))
{
count++;
s1=s1+"&"+j;
}
}
int before=0;
for(int x=i-1;x>0;x--)
{
if(s.charAt(i)==s.charAt(x))
before=1;
}
if(before==0)
System.out.println("the occurence of letter "+s.charAt(i)+" no of times= "+count+"--- positions="+s1);
}

}

}
}

Is This Answer Correct ?    5 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which data type is a class in java?

658


What are annotations in java?

729


What are accessor methods in java?

631


Is singleton a bad practice?

673


What is the difference between JDBC 1.0 and JDBC 2.0?

2799






What is the purpose of the strictfp keyword?

694


What is thread safe in java?

627


What comes to mind when someone mentions a shallow copy in java?

764


What is a 16 bit word?

659


Which is better stringbuilder or stringbuffer?

604


what state does a thread enter when it terminates its processing? : Java thread

695


How many ways can we create singleton class?

609


What are static blocks and static initalizers in java ?

653


What is an example of declaration?

632


Can a class have 2 constructors?

581