2

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

Community
  • 1
  • 1
Jeevan Roy dsouza
  • 653
  • 3
  • 12
  • 32

4 Answers4

7

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

Deepak
  • 2,287
  • 1
  • 23
  • 30
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
3
String s = "abcdef";
char c[] = s.toCharArray();

for( int i = c.length -1; i>=0; i--)
    System.out.print(c[i]);
laksys
  • 3,228
  • 4
  • 27
  • 38
3

Use StringBuilder class or StringBuffer class they have already a method reverse() for reversing the string

StringBuilder str = new StringBuilder("india");
System.out.println("string = " + str);

// reverse characters of the StringBuilder and prints it
System.out.println("reverse = " + str.reverse());

// reverse is equivalent to the actual
str = new StringBuilder("malayalam");
System.out.println("string = " + str);

// reverse characters of the StringBuilder and prints it
System.out.println("reverse = " + str.reverse());

http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

Deepak
  • 2,287
  • 1
  • 23
  • 30
1

Below is ugly hack. It concept but it not invoke any String methods.

import java.io.*;
import java.util.*;
public class Hello {
    public static String reverceWithoutStringMethods(String word){
        String result = "";
        //------ Write string to file -----------
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter( new FileWriter("tempfile"));
            writer.write(word);
        }
        catch ( IOException e) {}
        finally {
            try{
                if ( writer != null) writer.close( );
            }
            catch ( IOException e){}
        }
        //------ read string from file -------------
        RandomAccessFile f=null;
        try {
             f = new RandomAccessFile("tempfile", "r"); // Open file
            int length = (int) f.length(); // Get length
            // Read file
            byte[] data = new byte[length];
            f.readFully(data);
            // Reverse data
            for (int i=data.length; i>=0; i--){
                result += (char)data[i-1];
            }
        } catch(Exception e){}
        finally {
            try{
                f.close();
            }
            catch (Exception e){}
        }
        return result;
    }
    public static void main(String[] args) { 
        System.out.println(reverceWithoutStringMethods("Test"));
        System.out.println(reverceWithoutStringMethods(""));
    } 
}

Output:

tseT
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25