4 pts Write a method named printReverse that will accept a single (String parame
ID: 3701804 • Letter: 4
Question
4 pts Write a method named printReverse that will accept a single (String parameter and prints the String in reverse order For example, the following method call: rintReverse( Hello there!"): should print the following: ereht olleH If an empty string is passed in (i.e.,.." .), no output is produced. The method should print a single line of output. The method does not have a return value. Here is some Javadoc documentation for the method: " This method prints the reverse of a given String parameter " @param thing a String to print in reverseExplanation / Answer
public static void printReverse(String str){
String reverseStr = "";
//Checking if the string is null or empty
if(str!=null & str.trim().length()>0){
//loop to traverse string backwards
for(int i=str.length()-1;i>=0;i--){
//adding each char to another string
reverseStr = reverseStr+str.charAt(i);
}
//prints the reverse string
System.out.println(reverseStr);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.