How to print several strings backwards in Java

后端 未结 3 2013
青春惊慌失措
青春惊慌失措 2021-01-14 09:37

I am trying to take a file full of strings, read it, then print out a few things:

  • The string
  • The string backwards AND uppercase
  • The string len
3条回答
  •  我在风中等你
    2021-01-14 10:08

    This should do the job ->

    class ReverseWordsInString{
    public static String reverse(String s1){
            int l = s1.length();
            if (l>1)
                    return(s1.substring(l-1) + reverse(s1.substring(0,l-1)));
            else
                    return(s1.substring(0));
      }
    public static void main(String[] args){
            String st = "Cat Dog Rat";
            String r = "";
            for (String word : st.split(" "))
                    r += " "+ reverse(word.toUpperCase());
            System.out.println("Reversed words in the given string: "+r.trim());
      }
    }
    

提交回复
热议问题