ListIterator

Lab Java Advanced 30 points

Read n words into a LinkedList. Using a ListIterator, insert * after every word that starts with a capital letter and replace every word of length 1 with _. Print the list.

Input

n, then n words.

Output

The list.

Example

Input

5
Hello a big World x

Output

[Hello, *, _, big, World, *, _]
Sample input available Sample output available
Editor
Java

Hints

set() replaces the last returned element; add() inserts after it.

Solution explanation

ListIterator can modify a list in place while walking it, in either direction.

Show solution
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<String> words = new LinkedList<String>();
        for (int i = 0; i < n; i++) {
            words.add(sc.next());
        }
        ListIterator<String> it = words.listIterator();
        while (it.hasNext()) {
            String w = it.next();
            if (w.length() == 1) {
                it.set("_");
            } else if (Character.isUpperCase(w.charAt(0))) {
                it.add("*");
            }
        }
        System.out.println(words);
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD