1

Expected result:

input = "heLlo wOrLd"
output= "Hello World"

Actual result:

output= "Hello world"

My code only capitalizes the first word of the sentence:

public static void main(String args[]) {
    String input = "heLlo wOrLd";

    String output = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();

    System.out.println(output);
}
cegredev
  • 1,485
  • 2
  • 11
  • 25
  • @Schred the output i am getting : Hello world....Expected Output: Hello World –  Apr 29 '20 at 13:41
  • Use `.split(" ")` to break the sentence up based on spaces then apply your substring code to do the upper/lower casing to each String in the returned array. – Tim Hunter Apr 29 '20 at 13:42
  • 3
    Does this answer your question? [How to capitalize the first character of each word in a string](https://stackoverflow.com/questions/1892765/how-to-capitalize-the-first-character-of-each-word-in-a-string) – OH GOD SPIDERS Apr 29 '20 at 13:42
  • @OHGODSPIDERS no. In my case some characters are upper and lower case in between of word. I have to make that also to lowercase. –  Apr 29 '20 at 13:44
  • Your question title and your expected output are different – jhamon Apr 29 '20 at 13:47
  • 2
    Note that while the answers in the linked (possible) duplicate do only modify the first letter in each word, all you have to do to use one of the solutions in the linked question is do a `toLowerCase()` on your source string first (then use one of the methods to capitalize the first letter of every word). – OH GOD SPIDERS Apr 29 '20 at 13:47

4 Answers4

4

If you want to use streams, you could do this:

String output = Arrays.stream(input.split(" "))
                      .map(word -> word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase())
                      .collect(Collectors.joining(" "));
cegredev
  • 1,485
  • 2
  • 11
  • 25
1

This is fairly self-explanatory. It simply converts to a lower case string, splits on the white space and then replaces the first character of each word with the uppercase version.

String output = "";
for (String word : input.toLowerCase().split("\\s+")) {
    output += word.replaceFirst(".",word.substring(0, 1).toUpperCase()) + " ";
}
System.out.println(output);

or streams (but not as efficient as the first method). It does the exact same thing but calls a collector to reassemble the string by joining with a space.

String output = Arrays.stream(input.toLowerCase().split("\\s+"))
        .map(w -> w.replaceFirst(".",w.substring(0, 1).toUpperCase()))
        .collect(Collectors.joining(" "));

System.out.println(output);
WJS
  • 36,363
  • 4
  • 24
  • 39
0

Try this :

    String input = "heLlo wOrLd";

    String a []= input.split(" ");
    String output = "";
    for (String s:a){
        output = output + s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase() + " ";

    }
    System.out.println(output);
Sarangan
  • 868
  • 1
  • 8
  • 24
0

I presume that you'll try some other word sentences. This code is effective for any input.

        String input = scanner.nextLine();
        String[] array = input.split(" "); //separates the input by spaces
        String output = "";

        for (int i = 0; i < array.length; i++) {
            String word = array[i];
            String firstLetter = word.substring(0, 1).toUpperCase();
            String otherLetters = word.substring(1, word.length()).toLowerCase();

            word = firstLetter + otherLetters;
            output += word + " ";
        }

        System.out.println(output);