-2

I have been trying to make an Anagram solver. Where i read in a sentence, that i split into chars, spaces are allowed aswell. Then store them in a Char array. I have a List words i read in aswell. But am wondering if there is a good way to randommize the order in the char array, it has to consider not putting space at index 0 or index-max. I have read wordlist into a String array like this

String[] gh = File.ReadAllLines("words.txt");

this is just a simple consoleapplication, and it's only meant to be helping me solve the morningpapers anagram.

Edit: what i would like is a good way to swap around the letters of chars in a string?

Edit2: Maybe i should have been more specific, i don't want it to be entirely random. What a way to loop over and rearranging the chars, so i wont get the same word/words twice.

  • 3
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – sujith karivelil May 17 '17 at 05:55
  • 1
    _"is a good way to randommize the order in the char array"_ -- why would you want to include spaces when you randomize? Anagrams usually don't include spaces. Why randomize? Why not just create all permutations (lots of Q&A on Stack Overflow already that address permutations). What have you done so far? Are you asking Stack Overflow to design and implement your algorithm for you? Or do you have some _specific_ problem with some code you've already written? The former is too broad. If the latter, provide a good [mcve] showing clearly what you've got, and explain what you need help with. – Peter Duniho May 17 '17 at 06:00
  • my issue is i don't know how to make my char[] into random generated strings, could be using a bruteforcing kinda method, but how? and it can only use the chars in my char array. – user3759748 May 17 '17 at 06:01
  • preferable, just want somehow to mix/change pos of the chars in my char array, from there i think i can solver it. Preferable i don't want people from here doing my algorythem for me. – user3759748 May 17 '17 at 06:03
  • Would not it be nice if there are search engines that help google, yahoo, https://www.bing.com/search?q=c%23+randomize+array ? – Alexei Levenkov May 17 '17 at 06:07
  • 2
    I think that it's permutations you want instead of randomizations. It's a lot easier to go through all possible orders of the letters when you aren't generating them randomly. – Abion47 May 17 '17 at 06:11
  • See: [Listing all permutations of a string/integer](http://stackoverflow.com/questions/756055/listing-all-permutations-of-a-string-integer) – Abion47 May 17 '17 at 06:12
  • @Abion47 exatly, sorry for using "randomizations". My 1st lang isn't english. But thats exatly what i want. – user3759748 May 17 '17 at 06:13
  • @Abion47 thanks i found the answer i was looking for in the link you've shared. If u want you can write it as an answer and i will mark it as answered. – user3759748 May 17 '17 at 06:30

1 Answers1

1

What you want are permutations, not randomizations. To calculate all permutations of a list, the simplest way is via recursion:

private static void Swap(ref char a, ref char b)
{
    if (a == b) return;

    a ^= b;
    b ^= a;
    a ^= b;
}

public static void GetPer(char[] list)
{
    int x = list.Length - 1;
    GetPer(list, 0, x);
}

private static void GetPer(char[] list, int k, int m)
{
    if (k == m)
    {
        Console.WriteLine(list);
    }
    else
    {
        for (int i = k; i <= m; i++)
        {
            Swap(ref list[k], ref list[i]);
            GetPer(list, k + 1, m);
            Swap(ref list[k], ref list[i]);
        }
    }
}

static void Main()
{
    string str = "sagiv";
    char[] arr = str.ToCharArray();
    GetPer(arr);
}

(From Peter's answer)

Community
  • 1
  • 1
Abion47
  • 22,211
  • 4
  • 65
  • 88