Advertisement
jyoung12387

Scramble Word

Mar 20th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace switchStatementExample
  5. {
  6.                    
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             // declare a string to scramble, an empty list<char>.
  12.             // then add the chars of that string to the list
  13.             string word = "apple";
  14.             List<char> letterArray = new List<char>();
  15.             letterArray.AddRange(word);
  16.            
  17.             Random random = new Random();
  18.            
  19.             // randomly order the letters in the list<char>
  20.             for(int i = letterArray.Count; i > 0; i --)
  21.             {
  22.                 int index = random.Next(letterArray.Count);
  23.                 char currentChar =letterArray[index];
  24.                 Console.Write(currentChar);
  25.                 letterArray.RemoveAt(index);
  26.             }
  27.            
  28.             // output similar to
  29.             // aplep
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement