简体   繁体   中英

C# Reverse a string array without using sort( ) or reverse( )

Hi I am trying to write a method that will reverse a string array onced called. I finished my code , but only get half of the array reversed, leaving the rest unchanged, being stuck on this for hours. so I had to ask on stack as last resort.

int start;
string[] sArray = {
    "Reverse", "this", "string", "type", "array"
};
int end = (sArray.Length - 1);
for (start = 0; start < sArray.Length; start++) {
    sArray[start] = sArray[end - start];

    Console.Write(sArray[start] + ",");
}

//  The output supposed to be : array type string this Reverse
// However, I keep getting array type string type array.
//  The output supposed to be : array type string this Reverse
// However, I keep getting array type string type array.

Any ideas would be appreciated.

You are missing swapping. And you can do it with half of len of array:

string[] sArray = { "Reverse", "this", "string", "type", "array" };

for (int start = 0; start < sArray.Length/2; start++ )
{
     var temp = sArray[start];
     sArray[start] = sArray[sArray.Length - 1 - start];
     sArray[sArray.Length - 1 - start] = temp;                    
}

There are lots of way to do this.

First, you can use recursion. In C#-like pseudocode this will look like this:

T[] Reverse<T>(T[] input)
{
    if(input.Length <= 1) return input;
    return Reverse(input[0..input.Length - 1]) + input[..input.Length];
}

Next is an in-place reverse; almost what you've already done, except your for loop is twice as big as it needs to be. See what happens when you change one of the parts of your loop to start < sArray.Length / 2 . Plus, you really need to swap the elements.

You are rewriting items after the HALF to the first half. try this:

int start;
string[] sArray = { "Reverse", "this", "string", "type", "array" };
string[] temp = new string[sArray.Length];
int end = (sArray.Length-1);
for (start = 0; start < sArray.Length; start++ )
            {
                temp[start] = sArray[end - start];

                Console.Write(sArray[start]+",");
            }
sArray = temp; // putting back to the original varible

After half of the table you're swapping elements with already swapped. The proper solution is:

    static void Reverse()
    {
        string[] sArray = { "Reverse", "this", "string", "type", "array" };
        int end = sArray.Length - 1;
        for (int start = 0; start < (sArray.Length / 2); ++start)
        {
            string tmp = sArray[start];
            sArray[start] = sArray[end - start];
            sArray[end - start] = tmp;
        }

        foreach (var s in sArray)
            Console.Write(s + ",");
    }

Hopefully this answer makes sense, you can replace T by string if you don't understand generics. The end variable starts at the end, starts starts at the front of the array, and they progress closer to eachother until they point to the same element (for an odd sized array) or the end pointer points to something before the start pointer(for an even sized array), in either case start < end will return false, and the loop will stop.

private static void Reverse<T>(T[] items)
{
    for (int start = 0, end = items.Length - 1; start < end; start++, end--)
    {
        Swap(items, start, end);
    }
} 

private static void Swap<T>(T[] items, int a, int b)
{
    var help = items[a];
    items[a] = items[b];
    items[b] = help;
}

Sometimes I wonder why code looks more difficult than it should be. Try `

string[] words = new string[] {"reverse", "this", "string", "type", "array"};
string[] reverse = new string[words.Length];
int start = 0;`

for(int i = words.Length - 1; i >= 0; i--){
    reverse[start] = words[i];
    s++;
}

foreach(string s in reverse){
    Console.Write(s + ", ");
}

Hope this helps =) or use another for loop inside the for loop counting up instead of using start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM