Knoa Software Interview Question
Software Engineer / Developersexcellent solution xor 3 times
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
String s = "vijay";
String s1=reverse(s);
Console.WriteLine(s1);
Console.Read();
}
static String reverse(string str)
{
char[] charArray = str.ToCharArray();
int length = str.Length - 1;
for (int i = 0; i < length; i++, length--)
{
charArray[i] ^= charArray[length];
charArray[length] ^= charArray[i];
charArray[i] ^= charArray[length];
}
return new string(charArray);
}
}
}
Here is the Java code:
class reveseString{
public static void main(String args[])
{
String st = "yourString"; //Here you can replace yourString with some String
StringBuffer sbf = new StringBuffer(st);
int start=0,end=st.length()-1;
while(start<end)
{
sbf.setCharAt(start,(char)(sbf.charAt(start)^sbf.charAt(end)));
sbf.setCharAt(end,(char)(sbf.charAt(end)^sbf.charAt(start)));
sbf.setCharAt(start,(char)(sbf.charAt(start)^sbf.charAt(end)));
start++;
end--;
}
System.out.println(sbf);
}
}
- strezh September 14, 2010