Created
January 29, 2019 08:12
-
-
Save vignarajj/0ea8c9f7a6f1b1cca63adf62cd1800ea to your computer and use it in GitHub Desktop.
Capitalize First and Last character of String
This file contains 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
package javaproblems; | |
public class CaptializeChar { | |
public static String getCaptilizeString(String words){ | |
if(words.length()<=0){ | |
return "Invalid String"; | |
} | |
String[] splitWords = words.split("\\ "); | |
StringBuilder returnStr = new StringBuilder(words.length()); | |
for(int i =0; i <splitWords.length; i++){ | |
String firstChar = String.valueOf(Character.toUpperCase(splitWords[i].charAt(0))); | |
String btwn = splitWords[i].substring(1, splitWords[i].length()-1); | |
String lastChar = String.valueOf(Character.toUpperCase(splitWords[i].charAt(splitWords[i].length()-1))); | |
returnStr.append(firstChar).append(btwn).append(lastChar).append(" "); | |
} | |
return returnStr.toString(); | |
} | |
public static void main(String[] args){ | |
System.out.println(getCaptilizeString("hello world how are you")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment