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

Answers were Sorted based on User's Feedback



Write program to print Hello World and print each character address in that string and print how ma..

Answer / 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

Write program to print Hello World and print each character address in that string and print how ma..

Answer / 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

Write program to print Hello World and print each character address in that string and print how ma..

Answer / jishnu

Sorry I dont want to give a detailed answer here cos no one e is going to read the code.

You can use Map<Char,List<Integer>> counterMap

Iterate through the length of the string
for(int i=i; i<s.length();i++){
if(counterMap.get(s.getCharAt(i))==nul){
//First time
List<Integer> a= new ArrayList<Integer>();
a.add(i);
counterMap.put(s.getCharAt(i),a);
}else{
counterMap.get(s.getCharAt(i)).add(i);
}


//We can iterate through keySet or entrySet to show the result
}


Regards,
Jishnu

Is This Answer Correct ?    3 Yes 0 No

Write program to print Hello World and print each character address in that string and print how ma..

Answer / rahul verma

public static void main(String... arg)
{
String str="Guriqbal Singh";
int length = str.length();
System.out.println(str.length());
String tempStr="";
for(int i =0;i<length;i++)
{
String out="";
char a = str.charAt(i);

if(tempStr.contains(""+a))
continue;
tempStr=tempStr+a;
int count=0;
for(int j=0;j<length;j++)
{
if(a==str.charAt(j))
{
out = out+" At Loc : "+(j+1);
count++;
}
}
System.out.println(" CHar "+a+" No. of times:
= "+count+out);
}
}

Is This Answer Correct ?    1 Yes 0 No

Write program to print Hello World and print each character address in that string and print how ma..

Answer / 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

Write program to print Hello World and print each character address in that string and print how ma..

Answer / noor

Little change in the code

add this for loop
for (int x = i - 1; x >= 0; x--)

instead of
for (int x = i - 1; x > 0; x--)

Is This Answer Correct ?    0 Yes 0 No

Write program to print Hello World and print each character address in that string and print how ma..

Answer / binoy

I would make it simple.

public void printHello()
{

String s="Hello Good Morning";
int i=0;
for (char c: s.toCharArray())
{
System.out.print
(""+c+":"+i+count(c,s)+"\t");
i++;
}

}

public int count(char c, String s)
{
int index=-1;
int count =0;
while((index=s.indexOf(c+""))!=-
1)
{
s=s.substring(index+1);
count++;
}
return count;
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What are the advantages of functions?

0 Answers  


class A { public void disp(int a,int b) { System.out.println("hai"); } } class B { public void disp(int a,int b,int c) { System.out.println("hai"); } } above program is overloading or overriding?

9 Answers   Infosys, Wipro,


Can java list be null?

0 Answers  


Why can we not override static method?

0 Answers  


When do we use hashset over treeset?

0 Answers  






What is default locale java?

0 Answers  


What is the exception hierarchy in java?

0 Answers  


Can you pass functions in java?

0 Answers  


Why is java logo a cup of coffee?

0 Answers  


What is Map interface?

3 Answers   Wipro,


What are the steps in the jdbc connection?

0 Answers  


Can you inherit from an abstract class java?

0 Answers  


Categories