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
What is polymorphism and what are the types of it?
How does linkedhashmap work in java?
How is hashset defined in java?
How do you check if two given string are anagrams?
What is autoboxing in java?
What is the difference between a constructor and a method?
Why are the objects immutable in java?
what is the difference between yielding and sleeping? : Java thread
Outline the major features of java.
What is a line break?
What are Normalization Rules? Define Normalization?
What do you mean by stream pipelining in java 8?
What is the difference between Array and Hash Table?
What are nested classes in java?
What are java annotations?