Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified code: prev character is a space #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions 3_Strings/ConvertToUppercase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
//Problem : Convert all first letters of words to UPPERCASE
public class ConvertToUppercase {
public static String getUppercase(String str) {
StringBuilder newStr = new StringBuilder("");
char firstChar = Character.toUpperCase(str.charAt(0));
newStr.append(firstChar);
for(int i=0; i<str.length(); i++) {
if(str.charAt(i) == ' ' && i != str.length()-1) {
newStr.append(str.charAt(i));
i++;
StringBuilder newStr = new StringBuilder();
// space_found = true to capitalize first word
boolean space_found = true;
for (int i = 0; i < str.length(); i++) {
if (space_found) {
char ch = Character.toUpperCase(str.charAt(i));
newStr.append(ch);
} else {
newStr.append(str.charAt(i));
}
// every new word starts after a space
space_found = str.charAt(i) == ' ';
}

return newStr.toString();
return s.toString();
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Expand Down