Learn to Code
John F. Dumas
contact me | resume | how it works | example programs | testimonials | main page

Feedback Examples: One

one | two | three | four | five


► PROGRAMMING LANGUAGE:: C#

► ASSIGNMENT: The assignment is to author two methods:

   public static bool IsVowel(char c);
   public static string RemoveVowels(string s);

The first method should return true if 'c' is a vowel (you do not need to worry about upper/lower case, for now, you can assume any vowels you encounter will be lower case).

The second method should return back 's' but with all the vowels removed. You are to use 'IsVowel' inside of 'RemoveVowels' to decide if a given letter should be kept or removed from the result.

Here is a program with placeholder methods for 'IsVowel' and 'RemoveVowels'. Note that it contains a built-in test harness.

   using System;

   public class test
   {
      public static bool IsVowel(char letter)
      {
         return(false);
      }

      public static string RemoveVowels(string s)
      {
         return("");
      }

      public static void Main()
      {
         string[] list =
         {
            "abcdefghijklmnopqrstuvwxyz",
            "hello, is there anybody home?",
            "the quick brown fox jumps over the lazy dog"
         };

         foreach(string s in list)
         {
            Console.WriteLine("before: [{0}]", s);
            Console.WriteLine("after : [{0}]\n", RemoveVowels(s));
         }
      }
   }

Once your 'IsVowel' and 'RemoveVowels' methods are correct, your output should look like:

   before: [abcdefghijklmnopqrstuvwxyz]
   after : [bcdfghjklmnpqrstvwxyz]

   before: [hello, is there anybody home?]
   after : [hll, s thr nybdy hm?]

   before: [the quick brown fox jumps over the lazy dog]
   after : [th qck brwn fx jmps vr th lzy dg]

View the Student's Code


► FEEDBACK: Overall, this was a pretty solid effort. The most important thing is to get your code to produce the correct output and your program's output was right on track.

Here is your 'IsVowel' method:

      public static bool IsVowel(char letter)
      {
         if(letter == 'a' || letter == 'e' ||
            letter == 'i' || letter == 'o' || letter == 'u')
               return(true);
         else
               return(false);
      }

Nothing at all wrong with it, but notice that the method returns a 'bool' and the main portion of this method also deals with a boolean expression:

   (letter == 'a' || letter == 'e' ||
      letter == 'i' || letter == 'o' || letter == 'u')

In a scenario like this, you can dispense with the 'if' statment entirely and just return back the result of the boolean expression as in:

      public static bool IsVowel(char letter)
      {
         return(
            letter == 'a' || letter == 'e' ||
            letter == 'i' || letter == 'o' || letter == 'u'
         );
      }

And here is your 'RemoveVowels' method:

      public static string RemoveVowels(string s)
      {
         string result = "";

         foreach(char letter in s)
         {
            if(IsVowel(letter) != true)
               result += letter;
         }

         return(result);
      }

Again, there's nothing wrong with this code but the 'if' expression:

         if(IsVowel(letter) != true)

Seems a little more complex than it needs to be, what the expression above is saying is: "If this letter is not a vowel" (then add it to 'result'). This alternative is (I think) a bit more readable:

      public static string RemoveVowels(string s)
      {
         string result = "";

         foreach(char letter in s)
            if(!IsVowel(letter))
               result += letter;

         return(result);
      }


Reference Files


© John F. Dumas | johnfdumas@gmail.com | main page | top of page