using java Working with char arrays Given a char array build
using java
Working with char arrays. Given a char array, build a method that will print the chars in the array out in reverse order. Call your class “Reverse3”, and store it in a file called “Reverse3.java”.
public static void main(….) {
char name[] = {‘S’, ‘t’, ‘e’, ‘v’, ‘e’ };
print out the name array.
reverse3(name); //will print out chars backwards
}
You build the Reverse3 class and the reverse3() method. Make sure to make the method static.
Solution
public class Reverse3 {
/**
* @param args
*/
public static void main(String[] args) {
char name[] = { \'S\', \'t\', \'e\', \'v\', \'e\' };
// print out the name array.
System.out.print(\"Array elements:\");
for (int j = 0; j < name.length; j++) {
System.out.print(name[j] + \" \");
}
System.out.print(\"\ Reversed of an Array : \");
reverse3(name); // will print out chars backwards
}
/**
* method to print the char array in reverse order
*
* @param name
*/
public static void reverse3(char[] name) {
for (int i = name.length - 1; i >= 0; i--) {
System.out.print(name[i] + \" \");
}
}
}
OUTPUT:
Array elements:S t e v e
Reversed of an Array : e v e t S
