Created
October 26, 2015 20:03
-
-
Save JFFail/d96110fade12e9b8d986 to your computer and use it in GitHub Desktop.
Solution to Reddit Daily Programmer #238 - Consonants and Vowels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#https://www.reddit.com/r/dailyprogrammer/comments/3q9vpn/20151026_challenge_238_easy_consonants_and_vowels/ | |
#Create random "words" based on a defined input pattern. | |
#Define the alphabet. | |
$consonants= "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z" | |
$vowels = "a","e","i","o","u" | |
#Get input from the user. | |
$userInput = Read-Host "Enter the pattern" | |
$goodInput = $true | |
#Validate the input. | |
$userArray = $userInput.ToCharArray() | |
foreach($letter in $userArray) { | |
if(([string]$letter).ToLower() -ne "c" -and ([string]$letter).ToLower() -ne "v") { | |
Write-Host "Invalid input! Please use only 'c', 'C', 'v', and 'V'!" | |
$goodInput = $false | |
break | |
} | |
} | |
#Proceed if we've made it this far. | |
if($goodInput) { | |
$result = "" | |
foreach($letter in $userArray) { | |
if($letter -ceq 'c') { | |
#Select a random lowercase consonant. | |
$result += $consonants[(Get-Random -Minimum 0 -Maximum ($consonants.Length - 1))] | |
} elseif($letter -ceq 'C') { | |
#Select a random uppercase consonant. | |
$result += ($consonants[(Get-Random -Minimum 0 -Maximum ($consonants.Length - 1))]).ToUpper() | |
} elseif($letter -ceq 'v') { | |
#Select a random lowercase vowel. | |
$result += $vowels[(Get-Random -Minimum 0 -Maximum ($vowels.Length - 1))] | |
} elseif($letter -ceq 'V') { | |
#Select a random uppercase vowel. | |
$result += ($vowels[(Get-Random -Minimum 0 -Maximum ($vowels.Length - 1))]).ToUpper() | |
} | |
} | |
#Write the result. | |
Write-Host $result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment