Yesterday I had help via Stack Overflow on a program I written some code for, and needed to know if it needed to be improved.
After reviewing the responses, I restructured the code into the following...
using System;
namespace Language
{
public static class Grammar
{
/// <summary>
/// Returns a string value with non alpha/numeric characters removed.
/// </summary>
/// <param name="Sentence"></param>
public static string RemoveNonAlphaNumeric(string Sentence)
{
string[] Removed = { " ", ".", "!", "?", "@", "%", "&", "^", "$", "#", "*", "~" };
string[] words = Sentence.Split(Removed, StringSplitOptions.RemoveEmptyEntries);
return string.Join(" ", words);
}
/// <summary>
/// Returns the integer value of the number of words in a sentence.
/// </summary>
/// <param name="Sentence"></param>
/// <returns></returns>
public static int GetWordCount(string Sentence)
{
string[] Removed = { " " };
string[] Words = Sentence.Split(Removed, StringSplitOptions.RemoveEmptyEntries);
return Words.Length;
}
}
}
Initally GetWordCount() contained a foreach() loop which was basically counting the number of words in the array. I took the suggestion of someone removed this. Then replaced the return CountWords which was initially a variable declared at 0, with return words.Length. I also took out Stream as a string parameter as someone suggested that it may cause confusion.
Here's how the functions can be called from the main()...
using System;
using Language;
namespace ULESConMain
{
class Program
{
static void Main(string[] args)
{
string OldSentence = "@#The dirty dog, was &&walking proudly!";
// output using just Console.WriteLine() without assigning to a varaiable //
Console.WriteLine($"{Grammar.RemoveNonAlphaNumeric(OldSentence)}");
Console.WriteLine($"The total number of words in the sentence = {Grammar.GetWordCount(OldSentence)}");
// Output using Console.WriteLine() using returned values //
string NewSentence1 = Grammar.RemoveNonAlphaNumeric(OldSentence);
int WordCount = Grammar.GetWordCount(NewSentence1);
Console.WriteLine();
Console.WriteLine(NewSentence1);
Console.WriteLine($"The total number of words in the sentence = {WordCount}");
// Prompt to return control to the Operating System and exit the program.
Console.WriteLine("\nPress the ENTRER key to continue...");
Console.ReadKey(); // Get user input to return to the Operating System.
}
}
}
If anyone has any ideas or if you think this is good enough since it's working as expected. Please let me know.