String Array in Random Quote using C#

This tutorial, we will create and describes string arrays and it randoms the quotes shows how they work in C# .net. In C# .net, you can use strings as array of characters, However, more common practice is to use the string keyword to declare a string variable. This is the basic implementation for a newbie in programming who plans to build a system in the future.

Sample Code

String Arrays Sample code
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class GenericSample
  5. {
  6. public static void Main(string[] args)
  7. {
  8. List<string> list = new List<string>();
  9. list.Add("First string");
  10. list.Add("Second string");
  11. list.Add("Third string");
  12. list.Add("Fourth string");
  13. for(int n = 0; n < list.Count; n++)
  14. {
  15. Console.WriteLine(list[n]);
  16. }
  17. }
  18. }
For creating a random quote, here is the syntax.
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class RandomQuotes
  5. {
  6. static void CreateQuotes(List<string> quotes)
  7. {
  8. quotes.Add("I Hate Drugs!!!@Weed");
  9. quotes.Add("Long if its hot, short if its cold...@Popstickle");
  10. quotes.Add("John 3:16@Verse");
  11. quotes.Add("Good Bye!!!@Welcome");
  12. quotes.Add("HAHAHA :P !!!@Bleee");
  13. }
  14.  
  15. static void PrintQuote(string quote)
  16. {
  17. string[] substring = quote.Split('@');
  18. Console.WriteLine(substring[0]);
  19. Console.WriteLine("-" + substring[1]);
  20. }
  21.  
  22. static string GetRandomQuote(List<string> quotes)
  23. {
  24. int n = new Random().Next(0, quotes.Count);
  25. return(quotes[n]);
  26. }
  27.  
  28. public static void Main(string[] args)
  29. {
  30. List<string> list = new List<string>();
  31. CreateQuotes(list);
  32. string myRandomQuote = GetRandomQuote(list);
  33. if(args.Length == 0)
  34. {
  35. Console.WriteLine("Error Argument:\n * All\n * Random\n * Search");
  36. return;
  37. }
  38. if(args[0] == "random")
  39. {
  40. PrintQuote(myRandomQuote);
  41. }
  42. else if(args[0] == "all")
  43. {
  44. int x = 0;
  45. for(int n = list.Count; n > 0; n--)
  46. {
  47. PrintQuote(list[x]);
  48. x++;
  49. }
  50. }
  51. }
  52. }
Result Hope its help to all beginners and professional, Learn and enjoy the programming. Thanks!

Add new comment