In: Computer Science
In this assignment you will build a small C# project
that uses…
• A struct
• A method with a reference parameter
• A while-loop
• A switch statement and block
instructions:
Inside the StringHandler struct add a public void
method named Abbreviate.
The Abbreviate method must take a string parameter that is passed
by reference.
This method will take the name of a month (January, February, etc.)
as its input and
convert it to a 3-letter abbreviation of the month (JAN, FEB,
etc.).
Remember to use the ref keyword to specify a parameter that is passed by reference.
- Inside the Abbreviate method use a switch statement
to change the month name to its
uppercase 3-letter abbreviation. (January becomes JAN, February
becomes FEB, etc.)
-The Abbreviate method returns a void data type. So, we are not
returning a value.
Instead we are changing the value of the input parameter that has
been passed by
reference.
-If the value of the input parameter is not a valid month name,
then do not change it.
-in the Program.Main() method, use a while-loop along
with Console.Write() and
Console.ReadLine() to repeatedly ask the user to enter a month
name.
-When they enter “January” the console should follow by writing
“JAN”, when they enter
“February” it should follow by writing write “FEB”, etc. (See the
screenshot below.)
- When the user enters “QUIT” the loop should exit.
-The abbreviations should always be uppercase, but the user should
be able to enter the
month names in any combination of upper and lower case.
Do not write the code on paper, please type it
out.
Thank you !
Please find the following program in c# to abbreviate the user entered month to console
Note: I have added screen shots and comments inline for better understanding.
Program:
using System;
public class Program
{
public static void Main()
{
//infinite loop to iterate for ever, until
user enters QUIT
while (true)
{
StructHandler month;
//read the month
name
Console.Write("Enter the month:
");
month.name = Console.ReadLine();
//check if the user entered string is "QUIT"
if(month.name.Equals("QUIT"))
{
break;
}
string temp = month.name;
//call the Abbreviate method to convert the
months
month.Abbreviate(ref month.name);
//check if the month name is Abbreviated or not by
comparing it with
// the temp string
if (month.name.Equals(temp))
{
Console.WriteLine("Invalid month and not Abbreviated");
}
else
{
Console.WriteLine(month.name);
}
}
}
}
//StructHandler struct to handle conversion
struct StructHandler
{
public string name;
public void Abbreviate(ref string name)
{
//store the temp string with all upper case letters of name
variable
string temp = name.ToUpper();
switch(temp)
{
//use switch statements to match the months name and assign
only
//the first 3 characters of the month to name
variable
case "JANUARY":
name="JAN";
break;
case "FEBRUARY":
name="FEB";
break;
case "MARCH":
name="MAR";
break;
case "APRIL":
name="APR";
break;
case "MAY":
name="MAY";
break;
case "JUNE":
name="JUN";
break;
case "JULY":
name="JUL";
break;
case "AUGUEST":
name="AUG";
break;
case "SEPTEMBER":
name="SEP";
break;
case "OCTOBER":
name="OCT";
break;
case "NOVEMBER":
name="NOV";
break;
case "DECEMBER":
name="DEC";
break;
//dont change the name variable if the months are Invalid
default:
break;
}
}
}
Output:
Screen Shot: