1

I can't seem to find any solutions for this with just using String methods in Java. Having trouble trying to get the string out...

Heres my code:

import java.util.Scanner;
public class lab5_4
{
    public static void main(String[]args)
    {
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter a sentence");
        String s= scan.nextLine();
        String s2 = s.toLowerCase();

        for( int count = 0; count<= s2.length()-1; count++)
        {
            if( s2.charAt(count)==' ')
            {
                String s3 = s2.substring(count,count+1);
                String s4= s3.toUpperCase();
                System.out.print(s4);
            }
        }
    }
}
dur
  • 15,689
  • 25
  • 79
  • 125

3 Answers3

2

The following method forces all characters in the input string into lower case (as described by the rules of the default Locale) unless it is immediately preceded by an "actionable delimiter" in which case the character is coerced into upper case.

public static String toDisplayCase(String s) {

    final String ACTIONABLE_DELIMITERS = " '-/"; // these cause the character following
                                                 // to be capitalized
    
    StringBuilder sb = new StringBuilder();
    boolean capNext = true;

    for (char c : s.toCharArray()) {
        c = (capNext)
                ? Character.toUpperCase(c)
                : Character.toLowerCase(c);
        sb.append(c);
        capNext = (ACTIONABLE_DELIMITERS.indexOf((int) c) >= 0); // explicit cast not needed
    }
    return sb.toString();
}

TEST VALUES

a string

maRTin o'maLLEY

john wilkes-booth

YET ANOTHER STRING

OUTPUTS

A String

Martin O'Malley

John Wilkes-Booth

Yet Another String

Community
  • 1
  • 1
scottb
  • 9,908
  • 3
  • 40
  • 56
0

You seem to be checking for a whitespace and then calling toUpperCase() on that. You want s3 to be the next character, so it should be String s3 = s2.substring(count+1, count+2);.

You are also only printing characters where the if is evaluated as true instead of all characters. You need the print statement to be outside the if. This will require more than basic changes, but this should get you started.

Bobbyrogers
  • 302
  • 2
  • 10
0

you can use pattern to select the first letter from each sentence here an exemple

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter a sentence");
        String s= scan.nextLine();
        String toCappital = capitalTheFirstLetter(s);
        System.out.println(toCappital);

    }

    public static String capitalTheFirstLetter(String sentence){
        StringBuilder stringBuilder = new StringBuilder(sentence.toLowerCase());
        Pattern pattern = Pattern.compile("\\s?([a-z])[a-z]*"); // ([a-z]) to group the first letter
        Matcher matcher = pattern.matcher(sentence.toLowerCase()); 
        while (matcher.find()) {
            String fistLetterToCapital = matcher.group(1).toUpperCase();
            stringBuilder.replace(matcher.start(1), matcher.end(1), fistLetterToCapital); // replace the letter with it capital
        }
        return stringBuilder.toString();
    }

}

the output

Enter a sentence
how to capitalize the first letter of a string in a sentence ?
How To Capitalize The First Letter Of A String In A Sentence ?
Younes HAJJI
  • 375
  • 3
  • 21