A
A
Amir Kenesbay2021-08-18 21:12:37
Java
Amir Kenesbay, 2021-08-18 21:12:37

How to duplicate letters in a string?

For example, when typing the word
Important from the keyboard, the word Important should display duplicated words as shown below :

Scanner scanner = new Scanner(System.in);    
        String input = scanner.nextLine();     
        int sizeOfInput = input.length();

        for (int i = 0; i < sizeOfInput; i++) {
            int endIndex = i+1;
            String dup = input.substring(i, endIndex);
            System.out.print(dup + dup + dup);
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-08-18
@Amir1807

var multiplied = Arrays.stream("Important".split(""))
                .flatMap(s -> Stream.of(s, s, s))
                .collect(Collectors.joining());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question