Question

Could anybody give me comment here to help me understand this code? please, I want details...

Could anybody give me comment here to help me understand this code? please, I want details comment. thank you in advance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framing
{
class Program
{

static void Main(string[] args)
{
Dictionary charEncoding = new Dictionary();
charEncoding.Add("A", "01000111");
charEncoding.Add("B", "11100011");
charEncoding.Add("C", "11000001");
charEncoding.Add("ESC", "11100000");
charEncoding.Add("FLAG", "01111110");
Console.WriteLine("Enter Sequence: ");
string input = Console.ReadLine().Trim().ToUpper();
Console.Write("Byte count: " + Convert.ToString((input.Split(' ').Length + 1), 2).PadLeft(8, '0'));
foreach (string str in input.Split(' '))
{
if (!string.IsNullOrEmpty(str))
{
Console.Write(" "+charEncoding[str]);
}
}
Console.WriteLine();
Console.WriteLine("Flag bytes with byte stuffing: " + charEncoding["FLAG"]);
foreach (string str in input.Split(' '))
{
if (!string.IsNullOrEmpty(str))
{
Console.Write(" " + charEncoding[str]);
if (str.Equals("ESC"))
{
Console.Write(" " + charEncoding[str]);
}
}
}
Console.Write(" " + charEncoding["FLAG"]);
Console.ReadLine();

}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framing{
class Program
{

static void Main(string[] args){

/*Instantiate a new Dictionary. The Dictionary Class is present in the System.Collections.Generic;*/
Dictionary charEncoding = new Dictionary();

/*The name of the Dictionary is charEncoding. The words A, B, C, ESC, FLAG are stored in the dictionary.*/
charEncoding.Add("A", "01000111");
charEncoding.Add("B", "11100011");
charEncoding.Add("C", "11000001");
charEncoding.Add("ESC", "11100000");
charEncoding.Add("FLAG", "01111110");

/*This will print "Enter Sequence: " on the screen and is asking for an input.*/
Console.WriteLine("Enter Sequence: ");
//example input = " a c b esc flag "

/*The entered input is stored in the string variable named = input.
*Trim() method removes from the current string all leading and trailing white-space characters.
*ToUpper() method converts the input to all UPPERCASE.
*/

string input = Console.ReadLine().Trim().ToUpper();
//input = "A C B ESC FLAG"

/*Split() method will split the input after each occurence of ' ' i.e. space.
*The substrings will be stored in an array. The 1 in the (input.Split(' ').Length + 1) shows the size of the array in which each of these substrings
are going to be stored.*/

/*PadLeft(8, '0') will add zeroes to the string. It can be observed that the Dictionary contains encodings of length 8.
The format is str = str.PadLeft(8, '0');*/

/*Convert.ToString((input.Split(' ').Length + 1), 2) will return substrings and each of them will be padded with zeroes.*/
Console.Write("Byte count: " + Convert.ToString((input.Split(' ').Length + 1), 2).PadLeft(8, '0'));


/*This will be performed for each substring obtained previously*/
foreach (string str in input.Split(' ')){

if (!string.IsNullOrEmpty(str)){ //IsNullOrEmpty(str) returns TRUE if str is empty.
//Do this if string is NOT empty.
Console.Write(" "+charEncoding[str]);

//Print the given encoding for the str

}
}

//Print a new line
Console.WriteLine();
Console.WriteLine("Flag bytes with byte stuffing: " + charEncoding["FLAG"]);

/*This will be performed for each substring obtained by doing Split(' ')*/
foreach (string str in input.Split(' ')){
if (!string.IsNullOrEmpty(str)){
//Do this if string is NOT empty.
Console.Write(" " + charEncoding[str]);
//Print the given encoding of the str
if (str.Equals("ESC")){
Console.Write(" " + charEncoding[str]);
}
}
}

//This will print encoding for the FLAG.
Console.Write(" " + charEncoding["FLAG"]);

//This is probably written to exit when Enter is pressed.
Console.ReadLine();
}
}
}

/*The Code is clearly a bit incomplete but the definitions and the usage of the methods will remain the same as explained.*/

Add a comment
Know the answer?
Add Answer to:
Could anybody give me comment here to help me understand this code? please, I want details...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i need help fixing my code. here is the assignment: Make a class that contains the...

    i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...

  • Greetings everyone I am having a little trouble with this one. I bolded my switch case...

    Greetings everyone I am having a little trouble with this one. I bolded my switch case which should work but its currently not. If a user selected course option 1 twice it just says you enrolled in course 1 and course 1. It should stop it by executing case -2. Need a new set of eyes. - Thanks using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleRegisterStudent { class Program { static void Main(string[] args) { (new...

  • (C#, Visual Studio) I need help modifying my program code to accommodate repeated numbers. For example,...

    (C#, Visual Studio) I need help modifying my program code to accommodate repeated numbers. For example, if a user inputs a repeated number the program should regenerate or ask to enter again. If needed, here is the program requirements: Create a lottery game application. Generate four random numbers, each between 1 and 10. Allow the user to guess four numbers. Compare each of the user’s guesses to the four random numbers and display a message that includes the user’s guess,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT