Ultima attività 1753208667

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

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 // Example Output
9 // 1 Nephi 3:7: ___ it came __ ____ that I, Nephi said ____ __ father: I ____ go and do the things _____ the Lord hath commanded...
10 }
11}
12
13public class Scriptures
14{
15 private string[] scripture = new string[]
16 {
17 "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.",
18 "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..."
19 };
20 string[] separators = new string[] { " " };
21
22 private string GetRandomScripture()
23 {
24 var rand = new Random();
25 int index = rand.Next(scripture.Length);
26 return scripture[index];
27 }
28
29 public string GetRandomVerseWithHiddenWords()
30 {
31 var selectedScipture = GetRandomScripture();
32 string[] outPutverse = new string[selectedScipture.Length];
33 var splitVerse = selectedScipture.Split(separators, StringSplitOptions.None);
34
35 foreach (var item in splitVerse.Select((value, index) => new { index, value }))
36 {
37 var rand = new Random();
38 var tempValue = rand.Next(0, 20);
39 if (tempValue % 4 == 0)
40 {
41 var hiddenWord = new string('_', item.value.Length);
42 outPutverse[item.index] = hiddenWord + " ";
43 }
44 else
45 {
46 outPutverse[item.index] = item.value + " ";
47 }
48 }
49
50 return string.Concat(outPutverse);
51 }
52}