-2

Possible Duplicate:
Reverse “Hello World” in Java

How to print the reverse of a string?

string s="sivaram"; 

with out using the string handling functions

Community
  • 1
  • 1
SIVA
  • 11
  • 2
  • 6
  • 1
    Why without string functions? (Hint: If your starting point is a `String`, it is literally impossible without using at least **one** `String` function.) Is this some form of homework? What approaches have you tried? Also note that `String` is always initially capped in Java. – T.J. Crowder Jul 02 '12 at 17:30
  • why can't you use string handling functions? – Matt Westlake Jul 02 '12 at 17:30
  • @assylias: Big difference between this and that. This has the unreasonable, unexplained constraint of "no string functions". – T.J. Crowder Jul 02 '12 at 17:30
  • Can you treat it like an array? mychr = mystr[0]? – efesar Jul 02 '12 at 17:30
  • @T.J.Crowder There has to be one of the 15 answers that does that! – assylias Jul 02 '12 at 17:31
  • @assylias: Presumably not, as it's impossible. Directly or indirectly, you *will* use a `String` function to get the contents of the string. Directly (`toCharArray`) or indirectly via passing a string into `StringBuffer` or similar (which will call a `String` function). – T.J. Crowder Jul 02 '12 at 17:31
  • @ assylias no it's not... flip your screen across the vertical axis :-P – Matt Westlake Jul 02 '12 at 17:35
  • @T.J.Crowder If you can't access the string, then it's impossible but I doubt this is the intention of the question... – assylias Jul 02 '12 at 17:35
  • @assylias: Yes, I pointed this out above. The question is non-sensical. – T.J. Crowder Jul 02 '12 at 17:39

6 Answers6

3

All functions that access the contents of a String in Java are members of the String class, therefore all are 'string functions.' Thus, the answer to your question as written is 'it cannot be done.'

bmargulies
  • 97,814
  • 39
  • 186
  • 310
3

Assuming a strict interpretation of your question and that you can't use ANY of the methods provided by the String / StringBuilder classes (which I suppose is not the intention), you can use reflection to access the char array directly:

public static void main(String[] args) throws ParseException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    String s = "abc";
    Field stringValue = String.class.getDeclaredField("value");
    stringValue.setAccessible(true);
    char[] chars = (char[]) stringValue.get(s);

    //now reverse
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I strongly believe (judging from the question which is obviously homework) that they haven't reached reflection yet in the course – Cratylus Jul 02 '12 at 17:39
  • @user384706 in which case the most upvoted answer is not the expected one either! – assylias Jul 02 '12 at 17:40
  • 1
    @user384706: And yet, you have to give assylias credit for avoiding calling any string functions! Mind you, he does rely on undocumented features... :-) But seriously, the question is non-sensical, so using reflection and undocumented features in an answer isn't out of bounds. :-) – T.J. Crowder Jul 02 '12 at 17:41
  • @T.J.Crowder:The teacher will most likely not accept a solution using material not yet taught :P – Cratylus Jul 02 '12 at 17:42
  • 1
    @T.J.Crowder utterly evil, but the OP liked it ;-) – assylias Jul 02 '12 at 17:46
  • String.class.getDeclaredField("value"); isn't that a string function? – Matt Westlake Jul 02 '12 at 17:52
  • 1
    Nop, it is in the [Class class](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredField%28java.lang.String%29). – assylias Jul 02 '12 at 17:53
0

with out using the string handling functions

Sure. Get the underlying char[] and then use a standard C style reversal of the characters and then build a new String from the reversed char[]

char[] chars = s.toCharArray();
//Now just reverse the chars in the array using C style reversal  
String reversed = new String(chars);//done

I will not code this since this is definetely homework. But this is enough for you to get started

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • 1
    toCharArray() is a 'string function'. – bmargulies Jul 02 '12 at 17:33
  • @bmargulies:The `toCharArray` gets a reference to the underlying characters. It is not a "handling" method exactly.But you may have a point.It depends really on what the OP instructor had in mind – Cratylus Jul 02 '12 at 17:35
0

As far I am getting your question, this way should be working for you:

String str = "sivaram" ;//just an example (can be any string)
String newString = "" ;
for(int i=str.length()-1;i>-1;i--)
newString += str.charAt(i) ;
Dada
  • 6,313
  • 7
  • 24
  • 43
-1
public static void main(String args[]){
    char[] stringArray;
    stringArray = s.toCharArray();

    for(start at end of array and go to beginning)
        System.out.print( s.charAt( i));
}
j0ntech
  • 1,158
  • 3
  • 13
  • 27
Matt Westlake
  • 3,499
  • 7
  • 39
  • 80
-1

it's not possible to not use any string functions, I have a code that only uses two...

public String reverseString(String str)
{
    String output = "";
    int len = str.length();
    for(int k = 1; k <= str.length(); k++, len--)
    {
        output += str.substring(len-1,len);
    }
    return output;
}