settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Starts and Ends With Capital Letter - Java Task

+9 votes

Write a program that takes as input an array of strings are prints only the words that start and end with capital letter. Words are only strings that consist of English alphabet letters. Use regex.

Examples:

starts and ends with capital letters - java task

asked in Java category by user sam
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Here is the solution my friend (note the regular expression):

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Pr_06_StartsAndEndsWithCapitalLetter {
    public static void main(String[] args) {

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

        Pattern pattern = Pattern.compile("\\b([A-Z])+([a-zA-Z]+)?([A-Z])\\b");
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

Read here more: http://www.regular-expressions.info/wordboundaries.html and note that \b is a word boundary. It matches the beginning and ending of a word

answered by user golearnweb
edited by user golearnweb
...