Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Reverse a string using EX-OR operation in C#

Using exor operation also, we can reverse a string like below.

  1. using System;
  2. namespace forgetCode
  3. {
  4. class Program
  5. {
  6. public static void Main(string[] args)
  7. {
  8. Console.WriteLine("Enter the string to reverse :");
  9. string name = Console.ReadLine();
  10.  
  11. char[] charArray = name.ToCharArray();
  12. int len = name.Length - 1;
  13.  
  14. for (int i = 0; i < len; i++, len--)
  15. {
  16. charArray[i] ^= charArray[len];
  17. charArray[len] ^= charArray[i];
  18. charArray[i] ^= charArray[len];
  19. }
  20.  
  21. string newName = new string(charArray);
  22. Console.WriteLine(newName);
  23. }
  24. }
  25. }


Output:
Enter the string to reverse :
code

edoc