-5

I want to make code that simple: take word on English and make random array with all character of the word. for intense, the word:"qweasdzxc" should represent like that: "adwseqzcx" (random) . so the code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace randomLoop
{
  class Program
 {
    static void Main(string[] args)
    {
        string s = "qweasdzxc";
        string[] q = newArray(s);
        for (int i = 1; i < s.Length - 1; i++)
        {
            Console.Write(q[i]);
        }
    }

    public static char[] getArrayofstring(string s)
    {
        char[] c = s.ToCharArray();
        return c;

    }

    public static Random rnd = new Random();

    public static string[] charAndNumber(char c, int lengthOftheWord)//0-char 1-integer
    {
        string[] array = new string[2];
        array[0] = c.ToString();
        array[1] = (rnd.Next(lengthOftheWord)).ToString();
        return array;

    }

    public static string[] newArray(string s)
    {
        string[] c = new string[s.Length];
        int j = 1;
        string[] q = charAndNumber(s[j], s.Length - 1);

        for (int i = 1; i < c.Length - 1; i++)
        {
            c[i] = "";

        }
        int e = 1;

        while (e.ToString() != q[1])
        {

            c[e] = q[0];
            j++;//for the character s[j] see up
            e++;//for the loop
        }


        return c;
    }
}

}

  • 3
    Where exactly are you stuck at? – Adriano Carneiro May 16 '14 at 21:09
  • I don't see a question... – eddie_cat May 16 '14 at 21:09
  • You haven't asked a question! You've just posted some code that *sort of* looks like an attempt at solving the problem (but clearly doesn't even come close, just look at Main!) and haven't said where you are confused (besides having non-working code). We need more information. – BradleyDotNET May 16 '14 at 21:11
  • http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle – L.B May 16 '14 at 21:11
  • I think we all get that it's not running. What's not happening that you want to happen? Is an error being thrown? Which part don't you understand? – eddie_cat May 16 '14 at 21:12
  • if u can help me with your code – user3646157 May 16 '14 at 21:13
  • 3
    No kidding, but we aren't going to just write your program for you! Please explain where you are stuck/confused/lost as far as the next steps for your program. You have 3 functions, none of which call each other from what I can see, it doesn't look like a coherent attempt at solving the problem. Please help us help you and let us know something a bit more specific than "It doesn't work". – BradleyDotNET May 16 '14 at 21:13
  • I think he is attempting to make a [Substitution Cipher](http://en.wikipedia.org/wiki/Substitution_cipher). Is that what you are trying to do? – Scott Chamberlain May 16 '14 at 21:14
  • no i try to make from simple word to random with out ciphering – user3646157 May 16 '14 at 21:17
  • 1
    "qweasdzxc" is not an English word. It's a Cat word that describes the sound you make when your sexual organs are trapped in something. – Eight-Bit Guru May 16 '14 at 21:17

1 Answers1

0

It seems you just want to shuffle the characters and generate a new string.You can use the method that uses Fisher-Yates shuffle algorithm (from this answer, I modified it little bit for your situation):

public static void Shuffle<T>(this T[] source)  
{  
    Random rng = new Random();  
    int n = source.Length; 
    while (n > 1) {  
        n--;  
        int k = rng.Next(n + 1);  
        T value = source[k];  
        source[k] = source[n];  
        source[n] = value;  
    }  
}

Then just use it:

string s = "qweasdzxc";
char[] chars = s.ToCharArray();
chars.Shuffle();
string random = new string(chars);

Also remember that this is an extension method so you need to put it into a public and static class.

Community
  • 1
  • 1
Selman Genç
  • 100,147
  • 13
  • 119
  • 184