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 / binoy

I would make it simple.

public void printHello()
{

String s="Hello Good Morning";
int i=0;
for (char c: s.toCharArray())
{
System.out.print
(""+c+":"+i+count(c,s)+"\t");
i++;
}

}

public int count(char c, String s)
{
int index=-1;
int count =0;
while((index=s.indexOf(c+""))!=-
1)
{
s=s.substring(index+1);
count++;
}
return count;
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is difference between float and double?

512


how can you catch multiple exceptions in java?

549


Why Java doesn’t support multiple inheritance?

645


Differentiate between stringbuffer and stringbuilder in java.

559


when to use ArrayList and when to use HashMap in webApplication.

3900






What are peerless components?

635


Is java util list serializable?

545


How many digits can a float hold?

547


Explain about version control?

599


How many characters is 2 bytes?

538


Can you give few examples of final classes defined in java api?

569


What is files manifesting?

1742


How can we create objects if we make the constructor private ?

551


What is the purpose of an interface?

564


Why is it called buffering?

591