Advertisement
balrougelive

Untitled

Oct 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. /*
  2. |__________________________________________________________________________________________________________________________________|
  3. -METHS- [Text Analyzer Program]
  4.  
  5. Summary:
  6.  
  7. - Methods -
  8. - Overloading -
  9. - Optional Parameters -
  10.  
  11. Goal: Write a program that will analyze text.
  12.  
  13. Instructions: Write a function that will take a string parameter with "Hello" as the default value for the optional parameter.
  14.  
  15. This function will count the number of words in the string.
  16. - Assume that a word is identified by a single space character.
  17. - The return is the count of words.
  18.  
  19. Overload the above function by allowing it to take an array of strings:
  20. - There must be two separate functions.
  21.  
  22. Write a function that will count the number of vowels in a string:
  23. - The return is the count of vowels, case-insensitive.
  24.  
  25. Create a menu system similar to the sample output:
  26. - When at the bottom of each menu level, continue to perform the same operation until the user inputs the escape sequence. i.e. -1
  27. |__________________________________________________________________________________________________________________________________|
  28. */
  29.  
  30.  
  31. using System; // Spin up .NET libraries
  32. using System.Collections.Generic;
  33. using System.Linq;
  34. using System.Text;
  35. using System.Threading.Tasks;
  36.  
  37. namespace Meths_Text_Analyzer
  38. {
  39. class Program // Default Class named Program
  40. {
  41. static void Main(string[] args) // Enter Main:
  42. {
  43.  
  44. bool currently_Counting = true; // Setup a bool for the while loop default condition below:
  45.  
  46. while (currently_Counting) // While currently_Counting bool is true, run the code below:
  47.  
  48. {
  49.  
  50. Console.WriteLine("\nChoose from the following:"); // Setup Menu Options to user:
  51. Console.WriteLine("1. Word counting.");
  52. Console.WriteLine("2. Vowel counting.");
  53. Console.WriteLine("3. Exit");
  54. Console.Write("Enter your selection: ");
  55.  
  56. int user_Input = int.Parse(Console.ReadLine()); // Declare an int based on user input named user_Input.
  57.  
  58. switch (user_Input) // Switch to test a single expression againt multiple conditions for purposes of filling out menu options:
  59. {
  60. case 1: // (1. Word counting.) Condition for Word Counter Menu chosen:
  61.  
  62. Console.WriteLine("\nChoose from the following:"); // Menu Options to user for Word Counter Menu:
  63. Console.WriteLine("1. Count the words in one sentence.");
  64. Console.WriteLine("2. Count the words in a paragraph.");
  65. Console.WriteLine("3. Parent menu.");
  66. Console.Write("Enter your selection: ");
  67.  
  68. user_Input = int.Parse(Console.ReadLine()); // Update user_Input to the new user choice for case evaluation.
  69.  
  70. switch (user_Input) // Switch statement based on user_Input
  71. {
  72. case 1: // (1. Count the words in one setence.) Condition for counting the words in one sentence:
  73.  
  74. Console.WriteLine("Enter a sentence: (type q to quit, or type d for default):");
  75. Console.WriteLine(Word_Counter(Console.ReadLine())); // Call the Word_Counter method, which splits the string and returns the count as an int into the Console.
  76. break;
  77.  
  78. case 2: // (1. Count the words in one paragraph.) Condition for counting the words in one paragraph:
  79.  
  80. Console.WriteLine("Enter a paragraph (q to quit):");
  81.  
  82. Console.WriteLine(Word_Counter(ReadParagraph().Split())); // Call the overloaded Method Word_Counter, which takes the words of the paragraph and places them into an array of strings.
  83. break;
  84. }
  85.  
  86. break;
  87.  
  88. case 2: // (2. Vowel counting.) Condition for vowel counting menu chosen:
  89.  
  90. Console.WriteLine("\n1. Count the vowels in one sentence.");
  91. Console.WriteLine("2. Count the vowels in one paragraph.");
  92. Console.WriteLine("3. Exit.");
  93. Console.Write("Enter your selection: ");
  94.  
  95. user_Input = int.Parse(Console.ReadLine()); // Update user_Input to the new user choice for evaluation.
  96.  
  97. switch (user_Input) // Another switch statement which evaluates user_Input
  98. {
  99. case 1:
  100.  
  101. Console.WriteLine("Enter a sentence:");
  102. Console.WriteLine(Vowel_Count(Console.ReadLine())); // Call the Vowel_Count method
  103. break;
  104.  
  105. case 2:
  106.  
  107. Console.WriteLine("Enter a paragraph (type q to quit):");
  108. Console.WriteLine(Vowel_Count(ReadParagraph())); // Call the Vowel_Count Method and pass ReadParagraph Method into it.
  109. break;
  110. }
  111.  
  112. break;
  113.  
  114. default:
  115. currently_Counting = false; // Pass the bool into the default.
  116. break;
  117. }
  118. }
  119. }
  120.  
  121. static int Word_Counter(string input = "Hello") // Method named Word_Counter which returns int and takes string named input which is set to "Hello" by default
  122. {
  123. return input.Split().Length; // Split the input string string into an array of length word count which returns the length of the array/# of words.
  124. }
  125.  
  126. static int Word_Counter(string[] input) // Method named Word_Counter which returns the Length of the string
  127. {
  128. return input.Length;
  129. }
  130.  
  131. static int Vowel_Count(string input) // Method named Vowel_Count which returns int and takes a string named input. If the string contains a vowel, it splits the array.
  132. {
  133.  
  134. return System.Text.RegularExpressions.Regex.Split(input, "[AaEeIiOoUu]").Length - 1; // This splits the string using Regex funtion, and trims the length
  135.  
  136. }
  137.  
  138. static string ReadParagraph() // Method
  139. {
  140. string result = System.String.Empty; // Large string which is returned
  141. string input; // Current line which is being read
  142. bool can_Continue = true; // Setup a bool to allow the Paragraph parser to continue
  143.  
  144.  
  145. do
  146. {
  147. input = Console.ReadLine().Trim(); // Check for "q" user input to quit forming the paragraph.
  148. if (input == "q") // If we find q
  149. can_Continue = false; // Set can_Continue to false
  150. else
  151. result += " " + input; // Add the string to the result if q isn't found.
  152. } while (can_Continue); // The code runs when can_Continue is set to true.
  153.  
  154. return result.Trim(); // Trim function ensures we trim off whitespace on lines for the result string.
  155. }
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement