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 / nagvthu@gmail.com

public class WordCount {
public static void main(String args[])
{
String s = "HelloWorld";

for(int i=0; i<s.length(); i++) //Track
each character
{
int flag = 0, count = 0;
for(int j=i-1; j>=0; j--)
//This loop is for: If character repeats or Space
then skip to next character
{
if(s.charAt(j) == s.charAt
(i) || s.charAt(i) == ' ')
{
flag = 1;
break;
}
}
if(flag == 1)
continue;

System.out.print(s.charAt(i)
+ ": "); //Starts to check position and counter for
repeated characters
for(int k=i; k<s.length(); k++)
{
if(s.charAt(i) == s.charAt
(k))
{
count++;
if(k ==
i) //Just for nice output

System.out.print(k);
else

System.out.print("," + k);
}
}
System.out.println(" & " +
count); //end output line with count
}
}
}

Is This Answer Correct ?    6 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is main a function?

595


Difference between process and thread?

705


Define reflection.

643


What is java util concurrentmodificationexception?

586


What do you mean by synchronized non access modifier?

644






Write a program to print fibonacci series up to count 10.

590


How do you remove duplicates in java?

629


What is the major advantage of external iteration over internal iteration?

692


What are the types of methodology?

621


Explain the importance of throws keyword in java?

655


What is meant by nested loop?

646


What methods are used in Servlet?Applet communication?

1773


Explain what are final variable in java?

671


Can static methods access instance variables in java?

663


When will we use them?

689