Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse a string without using string functions [duplicate]

Tags:

java

How to write a java program to reverse a string without using string functions?

 String a="Siva";

 for(int i=0;i<=a.length()-1;i++)
 {
     System.out.print(a.charAt(i));
 }
     System.out.println("");

 for(int i = a.length() - 1; i >= 0; --i)
 {
     System.out.print(a.charAt(i)); 
 }

here charAt() and a.length() are string functions

like image 466
Jeevan Roy dsouza Avatar asked May 10 '26 03:05

Jeevan Roy dsouza


1 Answers

This will help

public class StringReverse {

   public static void main(String[] args){
    String str = "Reverse";
    StringBuilder sb = new StringBuilder(str);
    str = sb.reverse().toString();
    System.out.println("ReverseString : "+str);
  }

}

There is no usage of String methods

like image 138
Keerthivasan Avatar answered May 12 '26 16:05

Keerthivasan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!