Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe960d5
commit 7aa247d
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# String Reversal | ||
|
||
WAP to reverse a string using recursion | ||
|
||
**Example** | ||
|
||
``` | ||
input: abcd | ||
output: dcba | ||
``` | ||
|
||
## Solution | ||
|
||
### [JavaScript Implementation](./reverseString.js) | ||
|
||
```js | ||
/** | ||
* Reverse a string using recursion | ||
* Implemented by - MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
function reverseString (str) { | ||
if (str.length === 1) return str; | ||
return reverseString(str.substring(1, str.length)) + str[0]; | ||
} | ||
|
||
console.log (reverseString ('abcd')); | ||
``` | ||
|
||
### [Java Implementation](./ReverseString.java) | ||
|
||
```java | ||
/** | ||
* String Reversal using recursion | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
import java.util.Scanner; | ||
|
||
public class ReverseString { | ||
public static String reverse (String str) { | ||
if (str.length() == 1) return str; | ||
return reverse (str.substring(1, str.length())) + str.charAt(0); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner(System.in); | ||
System.out.println("/*/ ===== String Reversal using Recursion ==== */"); | ||
|
||
// Read the string | ||
System.out.print("\nEnter the string to be reversed: "); | ||
String str = input.nextLine(); | ||
|
||
// Print the result | ||
System.out.println("Reversed string is: " + reverse(str)); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* String Reversal using recursion | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
import java.util.Scanner; | ||
|
||
public class ReverseString { | ||
public static String reverse (String str) { | ||
if (str.length() == 1) return str; | ||
return reverse (str.substring(1, str.length())) + str.charAt(0); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner(System.in); | ||
System.out.println("/*/ ===== String Reversal using Recursion ==== */"); | ||
|
||
// Read the string | ||
System.out.print("\nEnter the string to be reversed: "); | ||
String str = input.nextLine(); | ||
|
||
// Print the result | ||
System.out.println("Reversed string is: " + reverse(str)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Reverse a string using recursion | ||
* Implemented by - MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
function reverseString (str) { | ||
if (str.length === 1) return str; | ||
return reverseString(str.substring(1, str.length)) + str[0]; | ||
} | ||
|
||
console.log (reverseString ('abcd')); |