Skip to content

Commit

Permalink
Add BONUS problems on recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 18, 2019
1 parent fe960d5 commit 7aa247d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
60 changes: 60 additions & 0 deletions BONUS/Recursion/ReverseString/README.md
@@ -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));
}
}
```
26 changes: 26 additions & 0 deletions BONUS/Recursion/ReverseString/ReverseString.java
@@ -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));
}
}
12 changes: 12 additions & 0 deletions BONUS/Recursion/ReverseString/reverseString.js
@@ -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'));

0 comments on commit 7aa247d

Please sign in to comment.