How to reverse a string

前端 未结 6 1256
北荒
北荒 2021-01-15 19:53

I need to reverse the string of a user\'s input.

I need it done in the simplest of ways. I was trying to do reverseOrder(UserInput) but it wasn\'t working.

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-15 20:22

    If you are new to programming, which I guess you are, my suggestion is "Why use simple stuff?". Understand the internals and play some!!

    public static void main(String[] args) {
    String str = "abcasz";
    char[] orgArr = str.toCharArray();
    char[] revArr = new char[orgArr.length];
    for (int i = 0; i < orgArr.length;i++)  {
    revArr[i] = orgArr[orgArr.length - 1 - i];
    }
    String revStr = new String(revArr);
    System.out.println(revStr);
    

提交回复
热议问题