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
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
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.'
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
}
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
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));
}
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;
}