0

The input is the source text, but the register of the first character of each word, which consists of three or more characters, must be inverted, and the word should be considered a sequence containing only letters (all other characters are not part of the word). For example : entrance: When I was younger I never needed exit : when I Was Younger I never needed

public static String upperWord(String input) {
    Pattern p = Pattern.compile("([\\p{InCyrillic}A-Za-z0-9-,]+\\s*)");
    Matcher matcher = p.matcher(input);
    StringBuilder builder = new StringBuilder();
    StringBuilder temp = new StringBuilder();
    while (matcher.find()) {
        temp.append(matcher.group());
        temp.setCharAt(0, String.valueOf(temp.charAt(0)).toUpperCase().charAt(0));
        builder.append(temp);
        temp.setLength(0);
    }
    return builder.toString();
}
Moon-flow
  • 1
  • 1

2 Answers2

0

Below would be my approach, it can be further optimised.

  1. Get the string.
  2. Check whether it is empty or not.
  3. If not empty split the sentence based on spaces so as to extract the individual words.
  4. Check the first character of each word and reverse it's case.
  5. Append the new word to the result string.
  6. Return the resultant string to the calling method.

public class StringManipulations {
        public static String formatString(String givenString) {
            StringBuilder resultBuilder = new StringBuilder();
            if (givenString.isEmpty()) {
                return givenString;
            } else {
                for (String word : givenString.split(" ")) {
                    if (word.length() >= 3) {
                        if (Character.isUpperCase(word.charAt(0))) {
                            word =
                                    Character.toLowerCase(word.charAt(0)) +
                                            word.substring(1);
                        } else if (Character.isLowerCase(word.charAt(0))) {
                            word =
                                    Character.toUpperCase(word.charAt(0)) +
                                            word.substring(1);
                        }
                        resultBuilder.append(word).append(" ");
                    } else {
                        resultBuilder.append(word).append(" ");
                    }
                }
                return resultBuilder.toString();
            }
        }

        public static void main(String[] args) {

            String originalString = "abc ef Ghi &";
            String formattedString = formatString(originalString);
            System.out.println(formattedString);

        }
    }
Sri9911
  • 1,187
  • 16
  • 32
  • Thank you, but what if you want the Cyrillic text to be processed too or how to make it processed (if possible)? example: INPUT: When I was younger\nI never needed\nПрощай, со всех вокзалов поезда\nуходят в Дальние Края OUTPUT: when I Was Younger\nI Never Needed\nпрощай, со Всех Вокзалов Поезда\nУходят в дальние края – Moon-flow Oct 02 '19 at 00:50
0

If you are using Java 8, you can achieve this with stream and Character.toUpperCase as follows:

String s0 = "When I was younger I never needed";
List<String> s1 = Arrays.asList(s0.split("\\s+"))
        .stream()
        .map(token -> Character.toUpperCase(token.charAt(0)) + token.substring(1))
        .collect(Collectors.toList());

System.out.println(String.join(" ", s1));

The console output is

When I Was Younger I Never Needed

LHCHIN
  • 3,679
  • 2
  • 16
  • 34