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 / meet parikh

Here you go,

public class Hello {
static String s = "";
static String word = "Hello World";
public static void main(String[] args){
for(int i=0; i<word.length();i++){
char tchar = word.charAt(i);
if(!checkCharContains(tchar)){
int count = 0;
StringBuilder sb = new StringBuilder();
for(int j=0;j<word.length();j++){
char ttchar = word.charAt(j);
if(ttchar == tchar){
count++;
sb.append(j).append(",");
}
}
sb.deleteCharAt(sb.length()-1);
s += tchar + " : " + sb.toString() + " & " +
count + " \n";
}
}
System.out.println(s);
}
public static boolean checkCharContains(char t){
boolean result = false;
for(int i=0;i<s.length();i++){
if(s.charAt(i) == t){
result = true;
}
}
return result;
}
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Does java isempty check for null?

664


What is connection class in java?

594


What is the importance of finally block in exception handling?

653


What is busy spin, and why should you use it?

688


What is int lol?

723






What is difference between length and length() method in java ?

657


What are accessor methods in java?

631


Why parsing is done?

612


What is a method in programming?

741


What is a finally block?

650


What is use of inner class in java?

622


How can we create a thread in java?

676


What are parameters in a method?

677


Why bytecode is called bytecode?

684


Which way a developer should use for creating thread, i.e. Sub classing thread or implementing runnable.

614