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

5
  • 2
    Do you want to reverse the string itself or just print it reversed? Commented Dec 5, 2013 at 6:52
  • 1
    just print it reversed Commented Dec 5, 2013 at 6:54
  • 3
    Without using any methods, you simply can't get at the data. Presumably this is homework, so I suggest you ask your tutor which methods you are allowed to use. Commented Dec 5, 2013 at 6:55
  • 1
    Without using any String methods we cant reverse a a string? Commented Dec 5, 2013 at 6:59
  • Refer learnjava-soa.blogspot.in/2016/02/… Commented Feb 15, 2016 at 7:17

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

str.toCharArray() is a string function
Pff, i went till. String str = "test"; Matcher m = Pattern.compile("$").matcher(str); m.find(); int length = m.end();
3
String s = "abcdef";
char c[] = s.toCharArray();

for( int i = c.length -1; i>=0; i--)
    System.out.print(c[i]);

5 Comments

length() is string function
It is length not length()
toCharArray() string functions.
s.toCharArray() array is a string function
Considering Java I think we cannot accomplish reverse without using functions since it does not support pointers directly.
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

Comments

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

3 Comments

charAt() word.length() are string functions
@JeevanRoydsouza see update.It not invoke any String method.
Yup we can do like this also.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.