3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Is it possible that examples of how both functions are called can be added to the review? \$\endgroup\$ – pacmaninbw May 4 at 22:07
  • 1
    \$\begingroup\$ I just re-edited the post which contains the main program with the calling examples. One note though. Normally one of the function parameters for string.split() is System.StringSplitOptions.RemoveEmptyEntries, this was done on purpose because i'm using the System namespace for now which allowed me to remove System from the function parameters. \$\endgroup\$ – Ause Sims May 4 at 23:00
1
\$\begingroup\$

The function RemoveNonAlphaNumeric promises to remove non-alphanumeric characters. It does this in an incomplete way.

  • The comma is not removed.
  • The apostrophe is not removed.
  • The em-dash — is not removed.

There's probably a function Character.IsDigit and Character.isLetter that is more appropriate. In the end, whether it is appropriate or not depends on what the code is supposed to do at all. What do you need it for, why do you want to remove non-alphanumeric characters in the first place?

\$\endgroup\$
  • \$\begingroup\$ They were actually left out on purpose. The string array variable Removed determines which characters are actually going to be removed from the string and then handled the string.split() method. The punctuation you mentioned will be added later to the array variable as I develop the rest of the code for a language translation program in which some languages doe not include these elements. This code is still in it's early stages and may change in further development of that application. However, thanks for the suggestion I may be able to use that in the rewrite should it come to it. \$\endgroup\$ – Ause Sims May 5 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.