Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- |__________________________________________________________________________________________________________________________________|
- -METHS- [Text Analyzer Program]
- Summary:
- - Methods -
- - Overloading -
- - Optional Parameters -
- Goal: Write a program that will analyze text.
- Instructions: Write a function that will take a string parameter with "Hello" as the default value for the optional parameter.
- This function will count the number of words in the string.
- - Assume that a word is identified by a single space character.
- - The return is the count of words.
- Overload the above function by allowing it to take an array of strings:
- - There must be two separate functions.
- Write a function that will count the number of vowels in a string:
- - The return is the count of vowels, case-insensitive.
- Create a menu system similar to the sample output:
- - When at the bottom of each menu level, continue to perform the same operation until the user inputs the escape sequence. i.e. -1
- |__________________________________________________________________________________________________________________________________|
- */
- using System; // Spin up .NET libraries
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Meths_Text_Analyzer
- {
- class Program // Default Class named Program
- {
- static void Main(string[] args) // Enter Main:
- {
- bool currently_Counting = true; // Setup a bool for the while loop default condition below:
- while (currently_Counting) // While currently_Counting bool is true, run the code below:
- {
- Console.WriteLine("\nChoose from the following:"); // Setup Menu Options to user:
- Console.WriteLine("1. Word counting.");
- Console.WriteLine("2. Vowel counting.");
- Console.WriteLine("3. Exit");
- Console.Write("Enter your selection: ");
- int user_Input = int.Parse(Console.ReadLine()); // Declare an int based on user input named user_Input.
- switch (user_Input) // Switch to test a single expression againt multiple conditions for purposes of filling out menu options:
- {
- case 1: // (1. Word counting.) Condition for Word Counter Menu chosen:
- Console.WriteLine("\nChoose from the following:"); // Menu Options to user for Word Counter Menu:
- Console.WriteLine("1. Count the words in one sentence.");
- Console.WriteLine("2. Count the words in a paragraph.");
- Console.WriteLine("3. Parent menu.");
- Console.Write("Enter your selection: ");
- user_Input = int.Parse(Console.ReadLine()); // Update user_Input to the new user choice for case evaluation.
- switch (user_Input) // Switch statement based on user_Input
- {
- case 1: // (1. Count the words in one setence.) Condition for counting the words in one sentence:
- Console.WriteLine("Enter a sentence: (type q to quit, or type d for default):");
- 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.
- break;
- case 2: // (1. Count the words in one paragraph.) Condition for counting the words in one paragraph:
- Console.WriteLine("Enter a paragraph (q to quit):");
- 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.
- break;
- }
- break;
- case 2: // (2. Vowel counting.) Condition for vowel counting menu chosen:
- Console.WriteLine("\n1. Count the vowels in one sentence.");
- Console.WriteLine("2. Count the vowels in one paragraph.");
- Console.WriteLine("3. Exit.");
- Console.Write("Enter your selection: ");
- user_Input = int.Parse(Console.ReadLine()); // Update user_Input to the new user choice for evaluation.
- switch (user_Input) // Another switch statement which evaluates user_Input
- {
- case 1:
- Console.WriteLine("Enter a sentence:");
- Console.WriteLine(Vowel_Count(Console.ReadLine())); // Call the Vowel_Count method
- break;
- case 2:
- Console.WriteLine("Enter a paragraph (type q to quit):");
- Console.WriteLine(Vowel_Count(ReadParagraph())); // Call the Vowel_Count Method and pass ReadParagraph Method into it.
- break;
- }
- break;
- default:
- currently_Counting = false; // Pass the bool into the default.
- break;
- }
- }
- }
- 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
- {
- 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.
- }
- static int Word_Counter(string[] input) // Method named Word_Counter which returns the Length of the string
- {
- return input.Length;
- }
- 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.
- {
- return System.Text.RegularExpressions.Regex.Split(input, "[AaEeIiOoUu]").Length - 1; // This splits the string using Regex funtion, and trims the length
- }
- static string ReadParagraph() // Method
- {
- string result = System.String.Empty; // Large string which is returned
- string input; // Current line which is being read
- bool can_Continue = true; // Setup a bool to allow the Paragraph parser to continue
- do
- {
- input = Console.ReadLine().Trim(); // Check for "q" user input to quit forming the paragraph.
- if (input == "q") // If we find q
- can_Continue = false; // Set can_Continue to false
- else
- result += " " + input; // Add the string to the result if q isn't found.
- } while (can_Continue); // The code runs when can_Continue is set to true.
- return result.Trim(); // Trim function ensures we trim off whitespace on lines for the result string.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement