CC-16 : Reverse String.
Description:
Given an input String, reverse the characters of the same and return String with reversed characters.
Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
| Original String: receive | Reversed String: eviecer |
| Reversed String: red | Reversed String: der |
Pseudocode:
reverseString(str):
| If str.length==0:
o return str.
|
| Else :
Set variable reversedChar to first character of the String.
Set variable reducedString to str with first character removed.
o Return string collated by appending return value from reverseString(reducedString) and variable reversedChar.
|
Code:
public String reverseString(String str) {
if (str.length() == 0) {
return str;
}else {
char reversedChar=str.charAt(0);
String reducedStr=str.substring(1);
return reverseString(reducedStr)+reversedChar;
}
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |