Menu
Topics Index
...
`

Program with BreakIterator


If you need explanation Read this topic

If you need Answer Take test on this topic

Program with BreakIterator
What will be the output of the following program?
import java.text.*;
import java.util.*;
public class CanYouBreak {
    public static void main(String[] args) {
        String data = "Merit Campus Question";
        String find = "Cam";
        BreakIterator bi = BreakIterator.getWordInstance(Locale.US);
        bi.setText(data);
        int temp = 0;
        int i = bi.first();
        while (i != BreakIterator.DONE) {
            int k = i;
            i = bi.next();
            if (i != BreakIterator.DONE && Character.isLetterOrDigit(data.charAt(k))) {
                String word = data.substring(k, i);
                System.out.println(word + "(" + k + ", " + i + ")");
                if (word.equalsIgnoreCase(find)) {
                    temp++;
                }
            }
        }
        System.out.println(find + " = " + temp);
    }
}

A. Merit(0, 4)
Campus(6, 11)
Question(13, 21)
Cam = 1
B. Merit (0, 4)
Campus (6, 11)
Question (13, 21)
Cam = 0
C. Some other output
D. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is C. Output is Merit(0,5)
Campus(6,12)
Question(13,21)
Cam=0
First time substring(k,i) returns Merit which is between the indices 0 and 5 next time Campus is returned with indices from 6 to 12 and so on. each value of word variable is chcked with 'Cam' which doesnt gets equal to any value. so temp is not incremented.

Posted by Nikhil Sulibhavi    2014-04-28 13:13:34


Congratulations Nikhil Sulibhavi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-04-29 04:08:37


© meritcampus 2019

All Rights Reserved.

Open In App