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 673189
In Process

The Archive Base Latest Questions

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

I need a C# function that takes 2 strings as an input and return

  • 0

I need a C# function that takes 2 strings as an input and return an array of all possible combinations of strings.

private string[] FunctionName(string string1, string string2) 
{
    //code
}

The strings input will be in the following format:

string1: basement

string2: a*fa

Now what I need is all combinations of possible strings using the characters in String2 (ignoring the * symbols), and keeping them in the same character position like this:

baaement, baaefent, baaefena, basefent, basemena, etc.

EDIT:
This is not homework. I need this function for a piece of a program I am doing.
The following is the code I have so far but it has some bugs.

static List<string> combinations = new List<string>();

static void Main(string[] args)
{
    //include trimming of input string
    string FoundRes = "incoming";
    string AltRes = "*2*45*78";
    List<int> loc = new List<int>();
    string word = "";


    for (int i = 0; i < AltRes.Length; i++)
    {
        if (AltRes[i] != '*')
        {
            loc.Add(i);
            word += AltRes[i];
        }
    }

    generate(word);
    string[] aaa = InsertSymbol(FoundRes, loc.ToArray(), AltRes, combinations);

    Console.WriteLine("input string: " + FoundRes);
    Console.WriteLine("Substitute string: " + AltRes);

    Console.WriteLine("============Output============");


    for (int j = 0; j < aaa.Length; j++)
    {

        Console.WriteLine(aaa[j]);
    }
    Console.ReadKey();
}//

private static void generate(string word)
{
    // Add this word to combination results set
    if (!combinations.Contains(word))
        combinations.Add(word);

    // If the word has only one character, break the recursion
    if (word.Length == 1)
    {
        if (!combinations.Contains(word))
            combinations.Add(word);
        return;
    }

    // Go through every position of the word
    for (int i = 0; i < word.Length; i++)
    {
        // Remove the character at the current position
        // call this method with the String
        generate(word.Substring(0, i) + word.Substring(i + 1));
    }
}//

private static string[] InsertSymbol(string orig, int[] loc, string alternative, List<string> Chars)
{
    List<string> CombinationsList = new List<string>();
    string temp = "";
    for (int i = 0; i < Chars.Count; i++)
    {
        temp = orig;
        for (int j = 0; j < Chars[i].Length; j++)
        {
            string token = Chars[i];

            if (alternative.IndexOf(token[j]) == loc[j])
            {
                temp = temp.Remove(loc[j], 1);
                temp = temp.Insert(loc[j], token[j].ToString());

                //     int pos = sourceSubst.IndexOf(token[j]);
                //     sourceSubst = sourceSubst.Remove(pos, 1);
                //     sourceSubst = sourceSubst.Insert(pos, ".");
            }
            else
            {
                temp = temp.Remove(alternative.IndexOf(token[j]), 1);
                temp = temp.Insert(alternative.IndexOf(token[j]), token[j].ToString());
            }
        }
        CombinationsList.Add(temp);
    }
    return CombinationsList.ToArray();
}//
  • 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-14T00:34:06+00:00Added an answer on May 14, 2026 at 12:34 am

    It does sound like homework. As a suggestion, I would ignore the first parameter and focus on getting all possible permutations of the second string. What’s turned off, what’s turned on, etc. From that list, you can easily come up with a method of swapping out characters of the first string.

    On that note, I’m in the uncomfortable position of having a function ready to go but not wanting to post it because of the homework implication. I’d sure love for somebody to review it, though! And technically, there’s two functions involved because I just happened to already have a generic function to generate subsets lying around.

    Edit: OP says it isn’t homework, so here is what I came up with. It has been refactored a bit since the claim of two functions, and I’m more than open to criticism.

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    class Program
    {
        static void Main()
        {
            string original = "phenomenal";
            string pattern = "*xo**q*t**";
    
            string[] replacements = StringUtility.GetReplacementStrings(original, pattern, true);
    
            foreach (string replacement in replacements)
                Console.WriteLine(replacement);
    
            Console.Read();
        }
    
        public static class StringUtility
        {
            public static string[] GetReplacementStrings(string original, string pattern, bool includeOriginal)
            {
                // pattern and original might not be same length
                int maxIndex = Math.Max(original.Length, pattern.Length);
    
                List<int> positions = GetPatternPositions(pattern, maxIndex, '*');
                List<int[]> subsets = ArrayUtility.CreateSubsets(positions.ToArray());
                List<string> replacements = GenerateReplacements(original, pattern, subsets);
    
                if (includeOriginal)
                    replacements.Insert(0, original);
    
                return replacements.ToArray();
            }
    
            private static List<string> GenerateReplacements(string original, string pattern, List<int[]> subsets)
            {
                List<string> replacements = new List<string>();
                char[] temp = new char[original.Length];
    
                foreach (int[] subset in subsets)
                {
                    original.CopyTo(0, temp, 0, original.Length);
                    foreach (int index in subset)
                    {
                        temp[index] = pattern[index];
                    }
    
                    replacements.Add(new string(temp));
                }
    
                return replacements;
            }
    
            private static List<int> GetPatternPositions(string pattern, int maxIndex, char excludeCharacter)
            {
                List<int> positions = new List<int>();
    
                for (int i = 0; i < maxIndex; i++)
                {
                    if (pattern[i] != excludeCharacter)
                        positions.Add(i);
                }
    
                return positions;
            }
        }
    
        public static class ArrayUtility
        {
            public static List<T[]> CreateSubsets<T>(T[] originalArray)
            {
                List<T[]> subsets = new List<T[]>();
    
                for (int i = 0; i < originalArray.Length; i++)
                {
                    int subsetCount = subsets.Count;
                    subsets.Add(new T[] { originalArray[i] });
    
                    for (int j = 0; j < subsetCount; j++)
                    {
                        T[] newSubset = new T[subsets[j].Length + 1];
                        subsets[j].CopyTo(newSubset, 0);
                        newSubset[newSubset.Length - 1] = originalArray[i];
                        subsets.Add(newSubset);
                    }
                }
    
                return subsets;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Configure your mail server to run on a database backend… May 14, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer I think you want SELECT * FROM x INNER JOIN… May 14, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer I think you have to also bring in entity a… May 14, 2026 at 11:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.