Challenge name: First Reverse
Difficulty: Easy
Maximum Score: 10
Solved language: Java
Description: For this challenge, you will be reversing a string.
Task: 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:
Input: "coderbyte"
Output: etybredoc
Input: "I Love Code"
Output: edoC evoL I
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()));
}
}
Code Explanation:
StringBuffer class: Java StringBuffer class is used to create mutable String objects. The StringBuffer class in Java is the same as String class except it is mutable.
reverse(): The Java.lang.StringBuffer.reverse() is an inbuilt method which is used to reverse the characters in the StringBuffer.