What's new
  • Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

C# C and C++ kunting tulong sa magaling mag c# jan?


network phantom

Grasshopper
using System;
using System.Linq;

namespace project
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter letter:");
string text = Console.ReadLine();
string cipherText = Encipher(text, 8);
string plainText = Decipher(cipherText, 8);
string Upp = cipherText.ToUpper();

Console.WriteLine($"Encrypt plain letter: {Upp}");
}

static string Encipher(string str, int num)
{
var res = str.Select(c =>
{
if (!char.IsLetter(c)) return c;
char offset = char.IsUpper(c) ? 'A' : 'a';
return (char)((((c + num) - offset) % 26) + offset);
});

return string.Concat(res);
}

static string Decipher(string input, int key)
{
return Encipher(input, 26 - key);
}
}
}
 
Comment

mrHazan

Eternal Poster
, Last edited:
Pag-aralan mabuti ang code.
This might not be the most efficient code but the outputs are correct.
Maganda dito pwede mo din iask ang user to replace 8. Para hindi 8 lang ang ciphering. Pwede any integer.

C#:
using System;

public class Program
{
    static string letters = "abcdefghijklmnopqrstuvwxyz";
    static string numbers = "0123456789";
    static string inputText = "";
    public static void Main()
    {
        Console.Write("Enter text: ");
        inputText = Console.ReadLine();
        Console.WriteLine("Encrypted: " + Cipher(inputText.ToLower(), 8));
        Console.Write("Enter text to decrypt: ");
        inputText = Console.ReadLine();
        Console.WriteLine("Decrypted: " + Cipher(inputText.ToLower(), -8));
    }

    public static string Cipher(string text, int num)
    {
        string cipheredText = "";
        int numMove = 0;
        for (int i = 0; i < text.Length; i++)
        {
            if (Char.IsWhiteSpace(text[i]))
            {
                cipheredText += ' ';
            }
            else
            {
                if (Char.IsNumber(text[i]))
                {
                    numMove = numbers.IndexOf(text[i]) + num;
                    if (numMove > numbers.Length - 1)
                    {
                        numMove = numMove % numbers.Length;
                    }
                    else if (numMove < 0)
                    {
                        while (numMove < 0)
                        {
                            numMove = numbers.Length + numMove;
                        }
                    }

                    cipheredText += numbers[numMove];
                }
                else
                {
                    numMove = letters.IndexOf(text[i]) + num;
                    if (numMove > letters.Length - 1)
                    {
                        numMove = numMove % letters.Length;
                    }
                    else if (numMove < 0)
                    {
                        while (numMove < 0)
                        {
                            numMove = letters.Length + numMove;
                        }
                    }
                    cipheredText += letters[numMove];
                }
            }
        }

        if (num < 0)
        {
            return cipheredText;
        }
        else
        {
            return cipheredText.ToUpper();
        }
    }
}
 
Comment

pixkit

Addict
What is the problem? How to write the code or you don't know the algorithm?

State the algorithm and I will code it for you. If it's the algorithm, then sorry I can't spoon fed that to you.
 
Comment

network phantom

Grasshopper
Pag-aralan mabuti ang code.
This might not be the most efficient code but the outputs are correct.
Maganda dito pwede mo din iask ang user to replace 8. Para hindi 8 lang ang ciphering. Pwede any integer.

C#:
using System;

public class Program
{
    static string letters = "abcdefghijklmnopqrstuvwxyz";
    static string numbers = "0123456789";
    static string inputText = "";
    public static void Main()
    {
        Console.Write("Enter text: ");
        inputText = Console.ReadLine();
        Console.WriteLine("Encrypted: " + Cipher(inputText.ToLower(), 8));
        Console.Write("Enter text to decrypt: ");
        inputText = Console.ReadLine();
        Console.WriteLine("Decrypted: " + Cipher(inputText.ToLower(), -8));
    }

    public static string Cipher(string text, int num)
    {
        string cipheredText = "";
        int numMove = 0;
        for (int i = 0; i < text.Length; i++)
        {
            if (Char.IsWhiteSpace(text[i]))
            {
                cipheredText += ' ';
            }
            else
            {
                if (Char.IsNumber(text[i]))
                {
                    numMove = numbers.IndexOf(text[i]) + num;
                    if (numMove > numbers.Length - 1)
                    {
                        numMove = numMove % numbers.Length;
                    }
                    else if (numMove < 0)
                    {
                        while (numMove < 0)
                        {
                            numMove = numbers.Length + numMove;
                        }
                    }

                    cipheredText += numbers[numMove];
                }
                else
                {
                    numMove = letters.IndexOf(text[i]) + num;
                    if (numMove > letters.Length - 1)
                    {
                        numMove = numMove % letters.Length;
                    }
                    else if (numMove < 0)
                    {
                        while (numMove < 0)
                        {
                            numMove = letters.Length + numMove;
                        }
                    }
                    cipheredText += letters[numMove];
                }
            }
        }

        if (num < 0)
        {
            return cipheredText;
        }
        else
        {
            return cipheredText.ToUpper();
        }
    }
}
maraming salamat po😊
 
Comment

network phantom

Grasshopper
What is the problem? How to write the code or you don't know the algorithm?

State the algorithm and I will code it for you. If it's the algorithm, then sorry I can't spoon fed that to you.
my problem is the prof want us to use only string, array and loop
 
Comment

pixkit

Addict
Unfortunately I think the core problem here for you is don't know the algorithm for the numbers. You can only start coding if you understand the algorithm

So basically to encrypt:
1. Get the next character from the input string
2. Convert to digit
3. Subtract 2 from the digit
4. If digit is less than 0, add 10 so it loops back to single digit
5. Convert to string and append to the final output

To decrypt:
1. Get the next character from the input string
2. Convert to digit
3. Add 2 to the digit (reverse of encrypt)
4. If digit is greater than 9, subtract 10 so it loops back to single digit
5. Convert to string and append to the final output

Here's a REPL for your reference
You do not have permission to view the full content of this post. Log in or register now.

Here's the source (I highly suggest you check the REPL link above instead):

C#:
using System;

public class Program {
  static string input = "";

  public static void Main() {
    Console.Write("Enter plaintext numbers: ");
    input = Console.ReadLine();
    Console.WriteLine("encrypted numbers: " + Encrypt(input));

    Console.Write("Enter encrypted numbers: ");
    input = Console.ReadLine();
    Console.WriteLine("Plaintext numbers: " + Decrypt(input));
  }

  public static string Encrypt(string input) {
    string encrypted = "";
    foreach (char c in input) {
      int encoded = int.Parse(c.ToString()) - 2;
      encoded = encoded < 0 ? encoded + 10 : encoded;
      encrypted += encoded.ToString();
    }
    return encrypted;
  }

  public static string Decrypt(string input) {
    string decrypted = "";
    foreach (char c in input) {
      int decoded = int.Parse(c.ToString()) + 2;
      decoded = decoded > 9 ? decoded - 10 : decoded;
      decrypted += decoded.ToString();
    }
    return decrypted;
  }
}
Sample output:
Bash:
Mono C# compiler version 4.6.2.0
 mcs -out:main.exe main.cs
 mono main.exe
Enter plaintext numbers: 0123456789
encrypted numbers: 8901234567
Enter encrypted numbers: 8901234567
Plaintext numbers: 0123456789
 
Comment

pixkit

Addict
Also try to avoid too many if-else or nested if-else statements
 
Comment

Like us on facebook

Online statistics

Members online
1,449
Guests online
1,007
Total visitors
2,456

Forum statistics

Threads
882,022
Messages
15,603,789
Members
1,409,899
Top