1. How to reverse a string in java without using reverse function
  2. Reverse a string in java
  3. Write a program to reverse string without using any function java
  • First of all "there is no direct built in function or method in string class to reverse a string" but still it is frequently asked question in java interviews.
  • It's not possible to reverse a string without using string methods. (length(),chgarAt())

public static String reverseString(String s) {
    char[] chars = s.toCharArray();
    int left = 0;
    int right = chars.length - 1;
    while (left < right) {
        char temp = chars[left];
        chars[left] = chars[right];
        chars[right] = temp;
    return new String(chars);

This method converts the input string to a character array using the toCharArray() method. Then, it initializes two pointers, left and right, to the first and last positions in the array, respectively.

Then, it enters a while loop that continues as long as the left pointer is less than the right pointer. In each iteration of the loop, the characters at the positions indicated by the left and right pointers are swapped using a temporary variable. Then, the left and right pointers are incremented and decremented, respectively.

Finally, the reversed character array is converted back to a string and returned.

This method is more efficient than using string functions because strings are immutable in Java and any operation that modifies the string creates a new object, whereas a character array can be modified in place.

reverse string without using string function

  • Lets discuss on how to reverse a string in java with using any built in string functions or methods.
  1. Using charAt() function/method

1:Using charAt():

  • Take a for loop and iterate string by checking length of the string from n to 1.
  • Use charAt() method of string and take each  character and append to another string.
  • By the end of the loop you will get all characters in reverse order.
  • Like this we can reverse a string in java with using string method.

#1: Reverse a string in java with using inbuilt function

  1. package com.instaceofjava;
  3. public class ReverseString {
  5. public static void main(String[] args) {
  7. String str="Hello world";
  8. String revstring="";
  10. for(int i=str.length()-1;i>=0;--i){
  11. revstring +=str.charAt(i);
  12. }
  14. System.out.println(revstring);
  15. }
  16. }


  1. dlrow olleH

  • This program reads a string from the user, and then uses a loop to iterate over the characters in the string in reverse order. It appends each character to a new string called reversed, and then prints out the reversed string after the loop has finished.
  • Note that this program does not modify the original string, but creates a new reversed string instead. If you want to reverse the original string in place, you can use an array of characters instead of a string, and swap the elements at opposite ends of the array until you reach the middle.
Here is a simple Java program that reverses a string without using any string functions:

  • This program works by first converting the input string to a character array using the toCharArray method. 
  • It then uses two pointers, i and j, to traverse the array from opposite ends. On each iteration, it swaps the characters at indices i and j, and increments i and decrements j until they meet in the middle. 
  • Finally, it converts the reversed character array back to a string using the String constructor.
2: Using StringBuffer Object:

Another way to reverse a string in Java without using any built-in string functions is to use a StringBuilder or StringBuffer object.

Here is an example using a StringBuilder:

public static String reverseString(String s) {
    StringBuilder sb = new StringBuilder(s);
    return sb.reverse().toString();

Here, the input string is passed to the constructor of the StringBuilder object. Then, the reverse() method is called on the object, which reverses the order of the characters in the string. Finally, the toString() method is called on the StringBuilder object to convert it back to a string and return it.

StringBuffer is similar to StringBuilder, it is thread safe, whereas StringBuilder is not.

public static String reverseString(String s) {
    StringBuffer sb = new StringBuffer(s);
    return sb.reverse().toString();

The above approach is more efficient than the previous one that used a character array because it avoids the need to create a temporary array and copy the characters to it.

In both cases, the method takes a single string parameter and returns the reversed version of it.

Java programming interview questions

  1. Print prime numbers? 
  2. What happens if we place return statement in try catch blocks 
  3. Write a java program to convert binary to decimal 
  4. Java Program to convert Decimal to Binary
  5. Java program to restrict a class from creating not more than three objects
  6. Java basic interview programs on this keyword 
  7. Interfaces allows constructors? 
  8. Can we create static constructor in java 
  9. Super keyword interview questions java 
  10. Java interview questions on final keyword
  11. Can we create private constructor in java
  12. Java Program Find Second highest number in an integer array 
  13. Java interview programming questions on interfaces 
  14. Top 15 abstract class interview questions  
  15. Java interview Questions on main() method  
  16. Top 20 collection framework interview Questions
  17. Java Interview Program to find smallest and second smallest number in an array 
  18. Java Coding Interview programming Questions : Java Test on HashMap  
  19. Explain java data types with example programs 
  20. Constructor chaining in java with example programs 
  21. Swap two numbers without using third variable in java 
  22. Find sum of digits in java 
  23. How to create immutable class in java 
  24. AtomicInteger in java 
  25. Check Even or Odd without using modulus and division  
  26. String Reverse Without using String API 
  27. Find Biggest substring in between specified character
  28. Check string is palindrome or not?
  29. Reverse a number in java?
  30. Fibonacci series with Recursive?
  31. Fibonacci series without using Recursive?
  32. Sort the String using string API?
  33. Sort the String without using String API?
  34. what is the difference between method overloading and method overriding?
  35. How to find largest element in an array with index and value ?
  36. Sort integer array using bubble sort in java?
  37. Object Cloning in java example?
  38. Method Overriding in java?
  39. Program for create Singleton class?
  40. Print numbers in pyramid shape?
  41. Check armstrong number or not?
  42. Producer Consumer Problem?
  43. Remove duplicate elements from an array
  44. Convert Byte Array to String
  45. Print 1 to 10 without using loops
  46. Add 2 Matrices
  47. Multiply 2 Matrices
  48. How to Add elements to hash map and Display
  49. Sort ArrayList in descending order
  50. Sort Object Using Comparator
  51. Count Number of Occurrences of character in a String
  52. Can we Overload static methods in java
  53. Can we Override static methods in java 
  54. Can we call super class static methods from sub class 
  55. Explain return type in java 
  56. Can we call Sub class methods using super class object? 
  57. Can we Override private methods ? 
  58. Basic Programming Questions to Practice : Test your Skill
  59. Java programming interview questions on collections

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
Newer Post
Older Post

No comments

Leave a Reply

Select Menu