Ultima attività 1753208667

An example of how to swap certain words for underscores from a given input array

Revisione 321b83b05f4b17a742f9f8289aa76de8d3016465

Program.cs Raw
1public class Program
2{
3 public static void Main()
4 {
5 var script = new Scriptures();
6 var verse = script.GetRandomVerseWithHiddenWords();
7 Console.WriteLine(verse);
8 }
9}
10
11public class Scriptures
12{
13 private string[] scripture = new string[]
14 {
15 "Mosiah 2:17: And behold, I tell you these things that ye may learn wisdom; that ye may learn that when ye are in the service of your fellow beings ye are only in the service of your God.",
16 "1 Nephi 3:7: And it came to pass that I, Nephi said unto my father: I will go and do the things which the Lord hath commanded..."
17 };
18 string[] separators = new string[] { " " };
19
20 private string GetRandomScripture()
21 {
22 var rand = new Random();
23 int index = rand.Next(scripture.Length);
24 return scripture[index];
25 }
26
27 public string GetRandomVerseWithHiddenWords()
28 {
29 var selectedScipture = GetRandomScripture();
30 string[] outPutverse = new string[selectedScipture.Length];
31 var splitVerse = selectedScipture.Split(separators, StringSplitOptions.None);
32
33 foreach (var item in splitVerse.Select((value, index) => new { index, value }))
34 {
35 var rand = new Random();
36 var tempValue = rand.Next(0, 20);
37 if (tempValue % 4 == 0)
38 {
39 var hiddenWord = new string('_', item.value.Length);
40 outPutverse[item.index] = hiddenWord + " ";
41 }
42 else
43 {
44 outPutverse[item.index] = item.value + " ";
45 }
46 }
47
48 return string.Concat(outPutverse);
49 }
50}