Tutorial C# Sharp - Learn to program with performing exercises C# Sharp

Tutorial C# Sharp

Learn to program with performing exercises C# Sharp

Encrypter - Tutorial C# Sharp
Tutorial C# Sharp4,85581245

Encrypter - Tutorial C# Sharp


Lesson 7:

More on classes


Exercise 7.4:

Encrypter


Objetive:

Create a class "Encrypter" to encrypt and decrypt text.

It will have a "Encrypt" method, which will receive a string and return another string. It will be a static method, so that we do not need to create any object of type "Encrypter".

There will be also a "Decrypt" method.

In this first approach, the encryption method will be a very simple one: to encrypt we will add 1 to each character, so that "Hello" would become "Ipmb", and to decrypt we would subtract 1 to each character.

An example of use might be

string newText = Encrypter.Encrypt("Hola");


Source Code:


using System;
namespace Program
{
    class Encrypter
    {
        public static string Encrypt(string text)
        {
            int letterInt = 0;
            char letter = ' ';
            string textEncripted = "";
            for (int i = 0; i < text.Length; i++)
            {
                letterInt = (int)text[i] + 1;
                letter = (char)letterInt;
                textEncripted += letter.ToString();
            }
            return textEncripted;
        }

        public static string Decrypt(string text)
        {
            int letterInt = 0;
            char letter = ' ';
            string textDecripted = "";
            for (int i = 0; i < text.Length; i++)
            {
                letterInt = (int)text[i] - 1;
                letter = (char)letterInt;
                textDecripted += letter.ToString();
            }
            return textDecripted;
        }
    }

    class TextEncripted
    {
        static void Main()
        {
            bool debug = true;

            string newText = Encrypter.Encrypt("Hola");
            Console.WriteLine("Text encripted: {0}", newText);
            string TextDescripted = Encrypter.Decrypt(newText);
            Console.WriteLine("Text Decripted: {0}", TextDescripted);

            if (debug)
                Console.ReadLine();
        }
    }
}
Exercisey 7.4






Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.