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 keyset in java?
Which of the following classes will have more memory allocated?
What is the difference between logical data independence and physical data independence?
What is compareto () in java?
What does string intern() method do?
What is the purpose of a volatile variable?
How to sort an array in java without using sort method?
What is ctrl m character?
How do you achieve polymorphism in java?
How are java objects passed to a method and what are native methods?
Can you tell me range of byte?
What is the primitive type byte?
When is the finally clause of a try-catch-finally statement executed?
Are primitives objects?
Can we print null in java?