Monday, July 29, 2019

Java Interview Question


Q #1) Write a Java Program to reverse a string without using String inbuilt function reverse().
Answer: 
Method 1:
There are several ways with which you can reverse your string if you are allowed to use the other string inbuilt functions.
In this method, we are initializing a string variable called str with the value of your given string. Then, we are converting that string into character array with toCharArray() function. Thereafter, we are using for loop to iterate between each character in reverse order and printing each character.
1public class FinalReverseWithoutUsingInbuiltFunction {
2    public static void main(String[] args) {
3        String str = "Saket Saurav";
4        char chars[] = str.toCharArray();  // converted to character array and printed in reverse order
5        for(int i= chars.length-1; i>=0; i--) {
6            System.out.print(chars[i]);
7        }
8    }
9}
Output:
varuaS tekaS

No comments:

Post a Comment

Which Python course is best for beginners?

Level Up Your Python Prowess: Newbie Ninjas: Don't fret, little grasshoppers! Courses like "Learn Python 3" on Codecade...