Skip to main content

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 [email protected]");
  9. quotes.Add("Long if its hot, short if its [email protected]");
  10. quotes.Add("John 3:[email protected]");
  11. quotes.Add("Good [email protected]");
  12. quotes.Add("HAHAHA :P [email protected]");
  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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.