-4

What I have so far is enter image description here

so if i were to input "Happy Holidays To YOU and YOUR Family" i want my output to be "happy holidays to you and your Family"

4
  • 1
    Please don't use images. Edit your question and add the code as text. Commented Sep 8, 2021 at 2:07
  • The essence of program design is to break down a problem into smaller parts. In that vein: downcase it all, then split into words, upcase the first character of the last word.. Code left as exercise for the student. Here's some useful documentation. Commented Sep 8, 2021 at 2:10
  • javascriptisnotjava.com Commented Sep 8, 2021 at 3:48
  • Please provide enough code so others can better understand or reproduce the problem. Commented Sep 13, 2021 at 15:07

3 Answers 3

2

Here is my Java solution using streams :

For input

"Happy Holidays To YOU and YOUR Family"

output is

"happy holidays to you and your Family"

public String convertToLowerCase(String str) {
   String delimiter = " ";
   String[] wordList =  str.split(delimiter);
   String lastWord = wordList[wordList.length - 1];
   return Arrays.stream(wordList).map(String::toLowerCase).limit(wordList.length - 1).collect(Collectors.joining(delimiter)) + delimiter + lastWord;
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a Javascript solution

let  myString = "Happy Holidays To YOU and YOUR Family"
let myFunction = (input) => {
  let arr = input.toLowerCase().split(" ");
  let str = arr[arr.length-1];
  arr[arr.length-1] = str[0].toUpperCase() + str.slice(1).toLowerCase();
  return arr.join(" ");
}

let newString = myFunction(myString)
console.log(newString)

Comments

0

Here is the answer:

/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
public static void main(String[] args) {
    String  myString = "Happy Holidays To YOU and YOUR Family";
    try{
        System.out.println(stringLowerCaseExpectLastWord(myString));
    } catch (IllegalArgumentException e){
        System.out.println(e.getMessage());
    }
}

/**
 * Upper first char at last word string.
 *
 * @param myString the my string
 * @return the string
 */
public static String stringLowerCaseExpectLastWord(String myString) {
    myString = myString.trim().toLowerCase();
    int i = myString.lastIndexOf(" ");
    if(i==-1)
        throw new IllegalArgumentException("No spaces in input");
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(myString.substring(0, i)
            .concat(" "));
    if(myString.length()==i+i){
        stringBuilder.append(myString.substring(i + 1).toUpperCase());
    }
    else {
        stringBuilder.append(myString.substring(i+1, i+2).toUpperCase()
                .concat(myString.substring(i+2)));

    }
    return stringBuilder.toString();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.