Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Methods | Structs & Enumerators
C# Beyond Basics

bookChallenge: Methods

The code contains a struct called Cypher which basically represents a kind of Caesar cipher.

Note
Note

The Caesar cipher is a text encoding method that makes messages unreadable by shifting each letter by a fixed number of positions in the alphabet or ASCII characters. For instance, shifting each character in "HelloWorld" by 7 positions according to the ASCII table, gives "Olssv^vysk." It's like a secret code where each letter is replaced by another letter a certain number of steps away. You can think of it as a simple way to hide messages or text.

You can read about Caesar cipher at it's Wikipedia Page, however, it's not necessary for solving this practice problem.

Read the code and fill in the blanks to make sure it works properly. The following is how the final program is supposed to be:

  1. setText method converts the passed text into encoded text and stores it into the field text. This method takes one string argument called text, and the method does not return any value;
  2. rawText returns the text field content. This method takes no arguments;
  3. decodedText decodes the text field content and returns the result. This method also doesn't take any arguments.
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344
using System; struct Cypher { public string text; ___ { string encodedText = ""; foreach(char chr in text) encodedText += (char) (chr + 7); ___ = encodedText; } public string rawText() { ___ } ___ { string decodedText = ""; foreach (char chr in this.text) decodedText += (char)(chr - 7); ___ } } class ConsoleApp { static void Main(string[] args) { Cypher text1 = new Cypher(); text1.setText("This is an example sentence."); Console.WriteLine(text1.rawText()); Console.WriteLine(text1.decodedText()); } }
  1. It is a good practice to use this keyword wherever you are referring to the struct fields.
  2. You use the void keyword as a return value in case the method doesn't return any value.
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344
using System; struct Cypher { public string text; public void setText(string text) { string encodedText = ""; foreach (char chr in text) encodedText += (char)(chr + 7); this.text = encodedText; } public string rawText() { return this.text; } public string decodedText() { string decodedText = ""; foreach (char chr in this.text) decodedText += (char)(chr - 7); return decodedText; } } class ConsoleApp { static void Main(string[] args) { Cypher text1 = new Cypher(); text1.setText("This is an example sentence."); Console.WriteLine(text1.rawText()); Console.WriteLine(text1.decodedText()); } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 7

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me the code that needs to be filled in?

What are the blanks that need to be completed in the code?

Can you explain how the encoding and decoding should work in this context?

Awesome!

Completion rate improved to 2.04

bookChallenge: Methods

Swipe to show menu

The code contains a struct called Cypher which basically represents a kind of Caesar cipher.

Note
Note

The Caesar cipher is a text encoding method that makes messages unreadable by shifting each letter by a fixed number of positions in the alphabet or ASCII characters. For instance, shifting each character in "HelloWorld" by 7 positions according to the ASCII table, gives "Olssv^vysk." It's like a secret code where each letter is replaced by another letter a certain number of steps away. You can think of it as a simple way to hide messages or text.

You can read about Caesar cipher at it's Wikipedia Page, however, it's not necessary for solving this practice problem.

Read the code and fill in the blanks to make sure it works properly. The following is how the final program is supposed to be:

  1. setText method converts the passed text into encoded text and stores it into the field text. This method takes one string argument called text, and the method does not return any value;
  2. rawText returns the text field content. This method takes no arguments;
  3. decodedText decodes the text field content and returns the result. This method also doesn't take any arguments.
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344
using System; struct Cypher { public string text; ___ { string encodedText = ""; foreach(char chr in text) encodedText += (char) (chr + 7); ___ = encodedText; } public string rawText() { ___ } ___ { string decodedText = ""; foreach (char chr in this.text) decodedText += (char)(chr - 7); ___ } } class ConsoleApp { static void Main(string[] args) { Cypher text1 = new Cypher(); text1.setText("This is an example sentence."); Console.WriteLine(text1.rawText()); Console.WriteLine(text1.decodedText()); } }
  1. It is a good practice to use this keyword wherever you are referring to the struct fields.
  2. You use the void keyword as a return value in case the method doesn't return any value.
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344
using System; struct Cypher { public string text; public void setText(string text) { string encodedText = ""; foreach (char chr in text) encodedText += (char)(chr + 7); this.text = encodedText; } public string rawText() { return this.text; } public string decodedText() { string decodedText = ""; foreach (char chr in this.text) decodedText += (char)(chr - 7); return decodedText; } } class ConsoleApp { static void Main(string[] args) { Cypher text1 = new Cypher(); text1.setText("This is an example sentence."); Console.WriteLine(text1.rawText()); Console.WriteLine(text1.decodedText()); } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 7
some-alt