Created
November 7, 2023 04:44
-
-
Save rafirh/13b688bf0b4b19463cafb0477f7da4dc to your computer and use it in GitHub Desktop.
Capitalize First
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
public static String capitalizeFirstLetter(String input) { | |
StringBuilder result = new StringBuilder(); | |
boolean capitalizeNext = true; | |
for (char c : input.toCharArray()) { | |
if (Character.isWhitespace(c)) { | |
result.append(c); | |
capitalizeNext = true; | |
} else { | |
if (capitalizeNext) { | |
result.append(Character.toUpperCase(c)); | |
capitalizeNext = false; | |
} else { | |
result.append(Character.toLowerCase(c)); | |
} | |
} | |
} | |
return result.toString(); | |
} | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
String str = input.nextLine(); | |
String result = capitalizeFirstLetter(str); | |
System.out.print(result); | |
input.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment