Reverse a given String in java without StringBuffer/Builder..
Intern Technical interview @ Virtusa
do some hackerrank and codility tests for algo related questions.
thanx ayya..I'm doing too..Reverse a given String in java without StringBuffer/Builder..
Intern Technical interview @ Virtusa
class riverse{
public static void main(String args[]){
String word = "Test";
//riverse = riverse(word);
System.out.println(riverse(word));
}
public static String riverse(String source){
String riverse= "";
for(int i= source.length() - 1; i>=0; i--){
riverse = riverse + source.charAt(i);
}
return riverse;
}
}
class riverse{
public static void main(String args[]){
String word = "Test";
//riverse = riverse(word);
System.out.println(riverse(word));
}
public static String riverse(String source){
StringBuilder riverse= new StringBuilder("");
for(int i = source.length()-1; i>=0; i--) {
riverse.append(source.charAt(i));
}
return riverse.toString();
}
}
class riverse{
public static void main(String args[]){
String word = "sourcecruos";
//riverse = riverse(word);
System.out.println(riverse(word));
}
public static boolean riverse(String source){
StringBuilder riverse= new StringBuilder("");
for(int i = source.length()-1; i>=0; i--) {
riverse.append(source.charAt(i));
}
return source.equals(reversedString.toString());
}
}


Swap two numbers stored in two integer variables without using a third variable.
owa nam gemak na
var a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
output balanna run karala![]()
owa nam gemak na
var a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
output balanna run karala![]()
owa nam gemak na
var a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
output balanna run karala![]()
Then swap two strings with out using a third variable.
EMINƎM;24264183 said:String a = "thinking";
String b = "guy";
a = a + b; //thinkingguy
b = a.substring(0, a.length()-b.length()); //thingking
a = a.substring(b.length()); //guy
System.out.println("a " + a);
System.out.println("b " + b);

EMINƎM;24264143 said:Is there any other method to swap the variables without using +,-,* or / ?
You are hired son.![]()



Xor swapping




then how to get value of b(after swaping)
look here carefully
if a = 10, b = 20;
a = a + b; -- line 01
b = a - b; -- line 02
a = a - b; -- line 03
-- line 01 output a = a + b = 10+ 20 = 30 , (still b = 20)
-- line 02 output b = a - b = 30 - 20 = 10, (still a = 30)
-- line 03 output a = a - b = 30 - 10 = 20
final output a = 20 , b = 10
still cannot understand
this is valid and no need for Xor swapping
