First Reverse Coderbyte Solution

1
Challenge name: First Reverse
Difficulty: Easy
Maximum Score: 10
Solved language: Java, Python, JavaScript, C++
Description: For this Challe challenge, you will be reversing a string.     

Problem

Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. 



For example: if the input string is "Hello World and Coders" then your program should return the string sredoC dna dlroW olleH.

Examples 1: 

Input: "coderbyte"
Output: etybredoc

Examples 2: 


Input: "I Love Code"
Output: edoC evoL I


Solution - First Reverse Java Coderbyte Solution


import java.util.*;
import java.io.*;

class Main {

public static String FirstReverse(String str) {
// code goes here
StringBuffer obj = new StringBuffer(str);
obj.reverse();
str = obj.toString();
return str;
}

public static void main(String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(FirstReverse(s.nextLine()));
}

}

Explanation:

In this Solution i used StringBuffer to find the reverse of given String.

1) Create StringBuffer object and pass the String value                  

2) Use reverse() of StringBuffer                                                  

3) Convert StringBuffer back to a string using toString().


Solution- First Reverse Coderbyte JavaScript


function FirstReverse(str) {

return str.split("").reverse().join("");
}

Explanation:

There is no reverse function for Strings in JavaScript BUT there is a reverse() function for arrays.  I will use this built-in function for my solution.

1) Convert the string to an array using the split() function.                     

2) Use Array Reverse() function.                                                  

3) Convert array back to a string using join() function. 


Solution- First Reverse Coderbyte Python

def FirstReverse(a_string):
reversed_string = ''
for char in a_string[::-1]:
reversed_string += char
return reversed_string


# keep this function call here
# to see how to enter arguments in Python scroll down
print FirstReverse(raw_input())


Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment
Our website uses cookies to enhance your experience. Learn More
Accept !