-3

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"

Nick
  • 16,066
  • 3
  • 16
  • 32
  • 1
    Please don't use images. Edit your question and add the code as text. – tgdavies Sep 08 '21 at 02: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](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html). – user16632363 Sep 08 '21 at 02:10
  • http://javascriptisnotjava.com/ – Basil Bourque Sep 08 '21 at 03:48
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 13 '21 at 15:07

3 Answers3

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;
 }
ARJUN KUMAR
  • 109
  • 1
  • 4
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)
rivrug
  • 177
  • 3
  • 8
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();
}
4EACH
  • 2,132
  • 4
  • 20
  • 28