How can I get a char array in reverse order?

2020-03-07 11:49发布

my assignment question is like that

Write a program which prints the letters in a char array in reverse order using

   void printReverse(char letters[], int size);

For example, if the array contains {'c', 's', 'c', '2', '6', '1'} the output should be "162csc".

I tried, but I don't know what it means

void printReverse(char letters[], int size);

I did this but there's problem with calling the method "printReverse" into main method

import java.util.Arrays;
import java.util.Collections;

public class search {

public static void main(String[] args) {          

char[] letters = {'e', 'v', 'o', 'l', '4'};
printReverse();

}

public void printReverse(char[] letters, int size){

for (int i = letters.length-1; i >= 0 ; i--){
System.out.print(letters[i]);
}
}

}

标签: java arrays char
8条回答
Luminary・发光体
2楼-- · 2020-03-07 12:39

void printReverse(char letters[], int size) is the signature of the function that you have to do. E.g.

void printReverse(char letters[], int size) {
//your code goes here
}

and call it from your main window with the parameters.

查看更多
够拽才男人
3楼-- · 2020-03-07 12:43

This is a class which reveres the character array using recursion.

public class ReverseString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String str = new String("Hello");
        char[] strcharArray = str.toCharArray();
        printReverse(strcharArray);

    }

    private static void printReverse(char [] str) {
          helper(0, str);
        }
        private static void helper(int index, char [] str) {
          if (str == null || index >= str.length) {
            return;
          }
          helper(index + 1, str);
          System.out.println(str[index]);
        }
}
查看更多
登录 后发表回答