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
How do I find and replace in word?
Why deletion in linkedlist is fast than arraylist?
Which is bigger double or float?
What is the difference between sleep and wait in java?
What is class level lock ?
What happens if a constructor is declared private?
How many decimal places is a double?
What is binary tree in java?
How can we create a thread in java?
What is default switch case? Give example.
what is the purpose of "virtual"?
What is meant by interface?
What does opcode mean?
What is difference between pointer and reference?
Can we use catch statement for checked exceptions when there is no chance of raising exception in our code?