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

Who found java?

657


Do loops java?

638


When will you define a method as static?

665


Which method returns the length of a string?

660


What is __ init __ functions?

652






Is java jre still free?

622


Is integer a class?

676


Can we overload destructor in java?

627


Which is better list or arraylist in java?

563


What are different exception types exceptions available in java ?

588


What is finalize method?

687


Can a class extends itself in java?

631


Explain garbage collection in java?

644


What is the use of keywords in java?

647


Write a program to find the whether a number is an Armstrong number or not?

670