Put spaces between the words in the string
- We can append between the words in the string using the methods available in the Character class and also by using the split method.
- After adding spaces convert the string to lowercase.
Example:
Input: KeeplearnersBlogspotCom
Output: keeplearners blogspot com
Program:
public class PutSpacesBtwWords {
public static void main(String[] args) {
String str = "KeeplearnersBlogspotCom";
System.out.print("Given Input String=" + str);
String newStr = "";
for (int i = 0; i <= str.length() - 1; i++) {
if (Character.isUpperCase(str.charAt(i))) {
newStr += " " + str.charAt(i);
} else {
newStr += str.charAt(i);
}
}
newStr = newStr.toLowerCase();
System.out.println("\nPut spaces between words starting with capital letters=");
System.out.print(newStr.trim());
String[] arr = str.split("(?=[A-Z])");
String finalStr = "";
for (String s1 : arr) {
finalStr += s1 + " ";
}
System.out.println("\nPut spaces between words starting with capital letters using split=");
System.out.print(finalStr.toLowerCase());
}
}
Output:
Put spaces between the words in string |
Please comment below to feedback or ask questions.
No comments:
Post a Comment
Please comment below to feedback or ask questions.