Sfida: Metodi
Il codice contiene una struct chiamata Cypher che rappresenta fondamentalmente una sorta di cifrario di Cesare.
Il cifrario di Cesare è un metodo di codifica del testo che rende i messaggi illeggibili spostando ogni lettera di un numero fisso di posizioni nell'alfabeto o nei caratteri ASCII. Ad esempio, spostando ogni carattere di "HelloWorld" di 7 posizioni secondo la tabella ASCII, si ottiene "Olssv^vysk." È come un codice segreto in cui ogni lettera viene sostituita da un'altra lettera a una certa distanza. Puoi considerarlo un modo semplice per nascondere messaggi o testo.
Puoi leggere del cifrario di Cesare sulla sua pagina Wikipedia, tuttavia non è necessario per risolvere questo esercizio.
Leggere il codice e completare gli spazi vuoti per assicurarsi che funzioni correttamente. Il programma finale dovrebbe essere il seguente:
- Il metodo
setTextconverte il testo passato in testo codificato e lo memorizza nel campotext. Questo metodo accetta un argomento di tipostringchiamatotexte non restituisce alcun valore; rawTextrestituisce il contenuto del campotext. Questo metodo non accetta argomenti;decodedTextdecodifica il contenuto del campotexte restituisce il risultato. Anche questo metodo non accetta argomenti.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344using 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()); } }
- È buona pratica utilizzare la parola chiave
thisogni volta che si fa riferimento ai campi della struct. - Si utilizza la parola chiave
voidcome valore di ritorno nel caso in cui il metodo non restituisca alcun valore.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344using 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()); } }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 2.04
Sfida: Metodi
Scorri per mostrare il menu
Il codice contiene una struct chiamata Cypher che rappresenta fondamentalmente una sorta di cifrario di Cesare.
Il cifrario di Cesare è un metodo di codifica del testo che rende i messaggi illeggibili spostando ogni lettera di un numero fisso di posizioni nell'alfabeto o nei caratteri ASCII. Ad esempio, spostando ogni carattere di "HelloWorld" di 7 posizioni secondo la tabella ASCII, si ottiene "Olssv^vysk." È come un codice segreto in cui ogni lettera viene sostituita da un'altra lettera a una certa distanza. Puoi considerarlo un modo semplice per nascondere messaggi o testo.
Puoi leggere del cifrario di Cesare sulla sua pagina Wikipedia, tuttavia non è necessario per risolvere questo esercizio.
Leggere il codice e completare gli spazi vuoti per assicurarsi che funzioni correttamente. Il programma finale dovrebbe essere il seguente:
- Il metodo
setTextconverte il testo passato in testo codificato e lo memorizza nel campotext. Questo metodo accetta un argomento di tipostringchiamatotexte non restituisce alcun valore; rawTextrestituisce il contenuto del campotext. Questo metodo non accetta argomenti;decodedTextdecodifica il contenuto del campotexte restituisce il risultato. Anche questo metodo non accetta argomenti.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344using 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()); } }
- È buona pratica utilizzare la parola chiave
thisogni volta che si fa riferimento ai campi della struct. - Si utilizza la parola chiave
voidcome valore di ritorno nel caso in cui il metodo non restituisca alcun valore.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344using 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()); } }
Grazie per i tuoi commenti!