Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# word randomizer

Random rndfruits = new Random();
string[] fruits = new string[] { "apple", "mango", "papaya", "banana", "guava", "pineapple" };
Console.WriteLine(fruits[rndfruits.Next(0,fruits.Length)]);
Comment

c# word randomizer


public void WordFinder()
{
    bool isWord = false;
    Random rnd = new Random();
    string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
    string[] vowels = { "a", "e", "i", "o", "u" };


    while (isWord == false)
    {
        string word = "";

        Console.WriteLine("Pick the length of a word");
        int requestedLength = Convert.ToInt32(Console.ReadLine());

        // Generate the word in consonant / vowel pairs
        while (word.Length < requestedLength)
        {
            if (requestedLength != 1)
            {
                // Add the consonant
                string consonant = GetRandomLetter(rnd, consonants);

                if (consonant == "q" && word.Length + 3 <= requestedLength) // check +3 because we'd add 3 characters in this case, the "qu" and the vowel.  Change 3 to 2 to allow words that end in "qu"
                {
                    word += "qu";
                }
                else
                {
                    while( consonant == "q")
                    {
                        // Replace an orphaned "q"
                        consonant = GetRandomLetter(rnd, consonants); 
                    }

                    if (word.Length + 1 <= requestedLength)
                    {
                        // Only add a consonant if there's enough room remaining
                        word += consonant;
                    }
                }
            }

            if (word.Length + 1 <= requestedLength)
            {
                // Only add a vowel if there's enough room remaining
                word += GetRandomLetter(rnd, vowels);
            }
        }

        Console.WriteLine(word);
        Console.WriteLine("Is this a word? (y/n)");
        string q = Console.ReadLine().ToLower();

        if (q == "y" || q == "yes")
        {
            isWord = true;
        }
    }
}

private static string GetRandomLetter(Random rnd, string[] letters)
{
    return letters[rnd.Next(0, letters.Length - 1)];
}

Comment

PREVIOUS NEXT
Code Example
Csharp :: regions unity 
Csharp :: net core get remote ip 
Csharp :: convert to base64 c# 
Csharp :: how to set a vector 3 variable in csharp 
Csharp :: c# combobox prevent typing 
Csharp :: check dotnet version command line 
Csharp :: get date of tomorrow c# 
Csharp :: unity get all by tag 
Csharp :: unity get layer of gameobject 
Csharp :: how t remove a component in unity 
Csharp :: how to find object by ag unity 
Csharp :: unity how to set an objects postion x,y,z 
Csharp :: blazor get current url 
Csharp :: Type is not marked as serializable. 
Csharp :: random bool c# 
Csharp :: c# check if string is empty 
Csharp :: initialise icollection c# 
Csharp :: unity why is there no transform.left 
Csharp :: create new gameobject unity 
Csharp :: unity c# get bool from another script 
Csharp :: move gameobject in unity 2d 
Csharp :: c# change label value into int 
Csharp :: unity detect number key 
Csharp :: how to change the extension of a file C# 
Csharp :: c# datediff minutes 
Csharp :: c# write to console 
Csharp :: unity rotate around pivot c# 
Csharp :: sum of two numbers in c# 
Csharp :: link nuttom in c# 
Csharp :: winforms messagebox with button 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =