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
What is string pooling concept?
What are the restrictions that are applied to the java static methods?
Which one of the following suits the description of a string better: derived or primitive?
Why string objects are immutable in java?
Can array grow dynamically in java?
What is the different between get and post?
What is the purpose of finalization in java programming?
How do you compare objects in java?
What is re-factoring in software?
How we can generate random numbers in java?
what is synchronization and why is it important? : Java thread
Mention some interfaces implemented by linked list in java.
How do you write a good declaration?
What does it mean to flush a file?
What is the covariant return type?