Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

What is e java?

1015


Is singleton class thread safe?

948


What happens if we don’t override run method ?

947


What is Java Shutdown Hook?

1112


What is javac_g?

967


What is super?

1046


Justify your answer that you can't define a method inside another method in java, if you can then how?

1074


What is the difference between Error, defect,fault, failure and mistake?

1132


What is anti pattern in java?

898


Is array size fixed in java?

918


Why there are some null interface in java? What does it mean?

1049


how to handle exceptions in ejb?

2259


What is the same as procedures?

926


What are inbuilt functions in java?

986


What is the epoch date?

942