1)Generally we can do reverse of String by using String API method toCharArray(). we call this method on String object then it will return char Array.
String stringVar = "Hai how are you man";
// Here we calling toCharArray() method on String object stringVar then arrayForOriginal // contains array of characters of that String
char arrayForOriginal[] = stringVar.toCharArray();
int originalArrLen = arrayForOriginal.length;
char arrayForReverse[] = new char[originalArrLen];
int reverseIndex = 0;
for(int indexVal = originalArrLen - 1; indexVal >= 0; indexVal--) {
arrayForReverse[ reverseIndex] = arrayForOriginal[indexVal] ;
reverseIndex++;
}
System.out.println( "Reverse of original String is: "+arrayForReverse);
Output:
Reverse of original String is: nam uoy era woh iah
String stringVar = "Hai how are you man";
/** Here we calling toCharArray() method on String object stringVar then arrayForOriginal * contains array of characters of that String
*/
*/
int originalArrLen = stringVar.length;
String reverseString = "";
int reverseIndex = 0;
for(int i = originalArrLen - 1;i >= 0;i--){
reverseString = reverseString+stringVar.charAt(i);
}
System.out.println( "Reverse of original String is: "+ reverseString);
Output:
Reverse of original String is: nam uoy era woh iaH
3) In this way we can do reverse of String without using String API. Means by using StringBuffer Class.Creating StringBuffer object as passing String value as argument to StringBuffer constructor. After calling reverse method of StringBuffer class on StringBuffer object.How we can do this programmatically, see below
String originalVal = "What are you doing now";
StringBuffer stringBufferVal = new StringBuffer(originalVal);
System.out.prinln(stringBufferVal.reverse());
String originalVal = "What are you doing now";
StringBuffer stringBufferVal = new StringBuffer(originalVal);
System.out.prinln(stringBufferVal.reverse());
Output:
Reverse of original String is: nam uoy era woh iaH
See also this posts:
octal to decimal in java
add two numbers each number has more than 1000 numbers
prime number
See also this posts:
octal to decimal in java
add two numbers each number has more than 1000 numbers
prime number