- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Java Source code: Reverse String in Java Using Recursion
Recursive Program on Reverse Letters/String
Reverse string in Java programming or printing a string backward using recursion is just a short program, which prompt the user to enter a string and the program output the last character up to the first character of the entered word. Originally, I used array on this but learned that it was not a wise decision so, I used the java predefined function charAt() instead. You can use charAt() if you want to extract a single character of the string. if you did not encounter charAt() yet, see the code below on how it works. Using charAt() is such a useful way to have a reverse string in java.
Java Source code: Reverse String in Java Programming
//Java source code on how to print a string backward using recursion //java class public class StringBackward { public static void reverseString(String word, int size) { if(size==0) { return; } else { System.out.print(word.charAt(size-1)); reverseString(word, size-1); } } } //main class import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String word; System.out.print("Enter a word: "); word = input.next(); StringBackward access = new StringBackward(); System.out.print("The reverse word is: "); access.reverseString(word, word.length()); System.out.println(); } }
Java Tutorials and Tips
- Java Tutorial Examples
- 5 Important Tips to Learn Java Programming and Other Programming Languages
- Basic Knowledge Required in Programming
- How to Program in Java: Complete Simple Easy Steps
- Java Simple Codes for Beginners
- Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
- Java Class: Learn More About Classes in Java
Below is the sample output of the program.
Sample Output:
Enter a word: hubpages
The reverse word is: segapbuh
Sample Output 2:
Enter a word: hubbers
The reverse word is: srebbuh
This is just how the program works, you can also make this program as a reference for a palindrome test using boolean. If the program is palindrome or the reverse word is the same as the original word return true and if not return false.
Other Java Source Codes
- Java Simple Codes for Beginners
- Java source codes on outputting Asterisk in Different Forms Using Recursion
- Java Source Code: A Recursive Asterisk Diamond Shape
- Java Source codes on Adding numbers inside the Array Using Recursion
- Java Source code on Adding Numbers inside the Array using For Loop
- Java Source Code: How to Sort Numbers in Bubble Sort Using Recursion
- Java Source Code: Sort Numbers using Selection Sort
- Java Source Code on Linear Search Using Recursion
- Java Source Code: Binary Search in Recursion
- Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
- Java source code: How to Output the Answer of the Integer X to the Power Y
- Java Source Code: How to make a Program that will determine a Person's Salutation and Current Age
- Java Source Code: Recursive Snow Flakes