Click here to Skip to main content
13,249,504 members (55,213 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 25 Jul 2011

Reverse of a string without using the Reverse function in C# and VB

, 25 Jul 2011
Rate this:
Please Sign up or sign in to vote.
/// /// Reverses an array (Change the type or use SetItem)/// /// The bytes/// The index in the bytes/// The length of bytes to reverseinternal static void Reverse(char[] array, int...
/// <summary>
/// Reverses an array (Change the type or use SetItem)
/// </summary>
/// <param name="array">The bytes</param>
/// <param name="index">The index in the bytes</param>
/// <param name="len">The length of bytes to reverse</param>
internal static void Reverse(char[] array, int index, int len)
    char temp;//Comment out when using XOR Swap
    //Length = 5 => {0,4}, {1,3}, ? {2,2} never hit because !(near < far)
    for (int near = index, far = len - 1; near < far; ++near, --far)
        temp = array[near]; //          array[near] ^= array[far];
        array[near] = array[far];//          array[far] ^= array[near];
        array[far] = temp;//          array[near] ^= array[far];
internal static void Reverse(char[] array)
   Reverse(array, 0, array.Length);
internal static String Reverse(String theString)
   char[] array = theString.ToCharArray();
   Reverse(array, 0, array.Length);
   return new String(array);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) ASTI Transportation Inc.
United States United States
Livin in a lonely world, caught the midnight train going anywhere... Only thing is it was a runaway train... and it ain't ever goin back...


You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 25 Jul 2011
Article Copyright 2011 by jfriedman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid