1

Sample string:

A3148579

Expected result:

798514A3

I tried this code:

 public static string Reverse(string s)
 {
     char[] charArray = s.ToCharArray();

     Array.Reverse(charArray);

     return new string(charArray);
 }

the actual result is 9758413A

but I want 798514A3

enter image description here

thanks everyone

1

3 Answers 3

1

You can split the initial string into chunks of size = 2 (e.g. with a help of Substring); note that ToCharArray() returns chars i.e. chunks of size = 1.

Let's generalize the solution: now we have size of chunks

Code:

using System.Linq;

...

public static string Reverse(string s, int size = 2) {
  if (size < 1)
    throw new ArgumentOutOfRangeException(nameof(size));

  if (string.IsNullOrEmpty(s))
    return s;
  
  int n = s.Length / size + (s.Length % size == 0 ? 0 : 1);

  return string.Concat(Enumerable
    .Range(0, n)
    .Select(i => i < n - 1 ? s.Substring(i * size, size) : s.Substring(i * size))
    .Reverse());
}

Demo:

Console.Write(Reverse("A3148579"));

Outcome:

798514A3
Sign up to request clarification or add additional context in comments.

Comments

1
You can try below code. This is just to give you a idea and you can update based on the test cases and requirement. Below code works fine for your input which you have mentioned. I have not considered if the length is odd. You can do your research and update logic which will help you to learn and know more.

string input = "A3148579";
            Stack stack = new Stack();
            int count = 0;
            string output = "";

            for (int i = 0; i < input.Length/2; i++)
            {
                stack.Push(input.Substring(count, 2));
                count = count + 2;
            }

            while (stack.Count > 0)
            {
                output += stack.Pop().ToString();
            }

Comments

0

C# Example: Split An Array into Multiple Smaller Arrays

Use Lambda expression to extends Array class to include a new method called Split:

public static class MyArrayExtensions
{
    /// <summary>
    /// Splits an array into several smaller arrays.
    /// </summary>
    /// <typeparam name="T">The type of the array.</typeparam>
    /// <param name="array">The array to split.</param>
    /// <param name="size">The size of the smaller arrays.</param>
    /// <returns>An array containing smaller arrays.</returns>
    public static IEnumerable<IEnumerable<T>> Split<T>(this T[] array, int size)
    {
        for (var i = 0; i < (float)array.Length / size; i++)
        {
            yield return array.Skip(i * size).Take(size);
        }
    }
}

Test the new array class method:

[TestMethod]
public void TestSplit2()
{
   string str = "A3148579";

    char[] array1 = new char[str.Length];

    for (int i = 0; i < array1.Length; i++)
    {
        array1[i] = i;
    }


    // Split into smaller arrays of maximal 2 elements
    IEnumerable<IEnumerable<int>> splited = array1.Split<int>(2);
    int j = 0;
    foreach (IEnumerable<int> s in splited){
        j++;
    }
    log.InfoFormat("Splitted in to {0} smaller arrays.", j);
}

Finally you just need to reverse the resulted array(smaller).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.