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]);
}
}
}
I believe what you wrote is the signature of the method you have to create.
You would have to iterate the array and print what it contains backwards. Use a reverse "for loop" to go through each item in "letters". I'll let you combine these yourself as it's an assignment. Here's an example of a for loop:
This should take about 6 ms. It reverses a char array "in-place" before printing.
You can make use of StringBuilder#reverse() method like this:
`
`
output: 4love
Man you code is right except some minor changes in the main method and in the loop and the method has to be static.
The signature printReverse(char[] letters, int size) means that when you call it, you have to pass char array and the size of the array
Try the following