Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 718905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:34:52+00:00 2026-05-14T05:34:52+00:00

I have a small algorithm which replaces the position of characters in a String:

  • 0

I have a small algorithm which replaces the position of characters in a String:

class Program
{
    static void Main(string[] args)
    {
        String pairSwitchedStr = pairSwitch("some short sentence");
        Console.WriteLine(pairSwitchedStr);

        Console.ReadKey();
    }

    private static String pairSwitch(String str)
    {
        StringBuilder pairSwitchedStringBuilder = new StringBuilder();
        for (int position = 0; position + 1 < str.Length; position += 2)
        {
            pairSwitchedStringBuilder.Append((char)str[position + 1]);
            pairSwitchedStringBuilder.Append((char)str[position]);
        }
        return pairSwitchedStringBuilder.ToString();
    }
}

I would like to make it as generic as possible, possibly using Generics. What I’d like to have is something which works with:

  • Anything that is built up using a list of instances.
  • Including strings, arrays, linked lists

I suspect that the solution must use generics as the algorithm is working on a list of instances of T (there T is … something). Version of C# isn’t of interest, I guess the solution will be nicer if features from C# version >2.0 is used.

I ended up with: (Where I fixed the error in the above code, it didn’t handle odd lengths correctly)

    private static IEnumerable<T> switchSubstitutionCipher<T>(IEnumerable<T> input)
    {
        bool even = false;
        T lastItem = default(T);

        foreach (T element in input)
        {
            if (even) {
                yield return element;
                yield return lastItem;
            }

            even = !even;
            lastItem = element;
        }
        if (even)
            yield return lastItem;
    }
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:34:52+00:00Added an answer on May 14, 2026 at 5:34 am
        private static IEnumerable<T> pairSwitch<T>(IEnumerable<T> input)
        {
            T current = default(T);     // just to satisfy compiler
            bool odd = true;
            foreach (T element in input)
            {
                if (odd) current = element;
                else
                {
                    yield return element;
                    yield return current;
                }
                odd = !odd;
            }
        }
    
        static void Main(string[] args)
        {
            string test = "Hello";
            string result = new string(pairSwitch(test).ToArray());
    

    Benefits:

    1. Lazy evaluation – which might be important for big lists

    2. String implementation is handled using IEnumerable implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.