0

Image HereI'm trying to do this program where when I input a letter into an inputfield, the output text shows the letter after the input text letter, like a should be b, or b should be c, and so on. So I thought I could make an string array for the alphabet, and if the input letter is equal to the index of the looping array, than the result would be equal to the index + 1. I tried it and its just giving me the same thing as the input. Am I missing something?

public class Encryption : MonoBehaviour
{
public InputField input;
public Text output;
string inputText;

public Toggle L337Toggle;
public Toggle CharShiftToggle;
public Toggle DoubleCaseToggle;
public Toggle VowelToggle;

void Start()
{
    L337Toggle.isOn = false;
    CharShiftToggle.isOn = false;
    DoubleCaseToggle.isOn = false;
    VowelToggle.isOn = false;
}
private void Update()
{
    inputText = input.text;

    output.text = inputText;


    IEncryption _textEncryption = new TextEncryption(inputText);
    IEncryption _L337Encryption = new L337Encryption(_textEncryption);
    IEncryption _CharShiftEncryption = new CharShiftEncryption(_textEncryption);


        if (L337Toggle.isOn == true)
        {
            output.text = _L337Encryption.Encrypt();
        }
        else if (CharShiftToggle.isOn == true)
    {
        output.text = _CharShiftEncryption.Encrypt();
    }
}


public interface IEncryption
{
    string Encrypt();
}

public class TextEncryption : IEncryption
{
    private string originalString;

    public TextEncryption(string original)
    {
        originalString = original;
    }
    public string Encrypt()
    {
        Debug.Log("Encrypting Text");
        return originalString;
    }
}

public class L337Encryption : IEncryption
{
    private IEncryption _encryption;

    public L337Encryption(IEncryption encryption)
    {
        _encryption = encryption;
    }
    public string Encrypt()
    {
        Debug.Log("Encrypting L337 Text");
        string result = _encryption.Encrypt();
        result = result.Replace('a', '4').Replace('b', '8').Replace('e', '3').Replace('g', '6').Replace('h', '4').Replace('l', '1')
            .Replace('o', '0').Replace('q', '9').Replace('s', '5').Replace('t', '7').Replace('A', '4').Replace('B', '8')
            .Replace('E', '3').Replace('G', '6').Replace('H', '4').Replace('L', '1')
            .Replace('O', '0').Replace('Q', '9').Replace('S', '5').Replace('T', '7'); 

        return result;
    }
}


public class CharShiftEncryption : IEncryption
{
    private IEncryption _encryption;
    public CharShiftEncryption(IEncryption encryption)
    {
        _encryption = encryption;
    }
    public string Encrypt()
    {
        Debug.Log("Encrypting Character Shift Text");
        string result = _encryption.Encrypt();
        string[] Letters=new string[] { "a", "b'", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
            "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

        for (int i=0;i<Letters.Length;i++)
        {
            if (Letters[i] == result)
            {
                result = Letters[i + 1];
          if (result == Letters[25])
         {
                result = Letters[0];
                }
            }
        }
        return result;
    }
}
4
  • Your if statement is never going to return true. Because result never gets set, because your if statement never returns true, its a circle. EDIT: Aren't you getting an error that it is a infinite loop? Commented Oct 19, 2017 at 13:31
  • Just added a screenshot Commented Oct 19, 2017 at 13:34
  • What is the correct letter if you pass z? You also only check lower case letters. Commented Oct 19, 2017 at 13:35
  • I know, I was going to make another array for Caps, and if the index > letters.length, then i'll set i=0 Commented Oct 19, 2017 at 13:36

2 Answers 2

1

I suggesting using Dictionary, not an array. First create it:

 using System.Linq;

 ...

 // {{'a', 'b'},
 //  {'b', 'c'},
 //   ...
 //  {'y', 'z'},
 //  {'z', 'a'}}
 const int range = 'z' - 'a' + 1;

 static Dictionary<char, char> s_Dict = Enumerable
   .Range('a', range) // all letters enumerated
   .Select(item => new {
      key = (char) item,
      value = (char) ('a' + (item - 'a' + 1) % range),  
    }) 
   .ToDictionary(item => item.key,
                 item => item.value);

Then use it

  public static string Encrypt(string source) {
    if (null == source)
      return null; // or throw ArgumentNullException  

    // out var - C# 7.0 syntax
    return string.Concat(source
      .Select(c => s_Dict.TryGetValue(c, out var cNew) // do we have a key?
         ? cNew // if true, put the corresponding value 
         : c)); // if false, leave the character intact
  }

Test:

  string test = "encrypt me, please!";

  Console.WriteLine(test);
  Console.WriteLine(Encrypt(test));

Outcome:

  encrypt me, please!  
  fodszqu nf, qmfbtf!
Sign up to request clarification or add additional context in comments.

5 Comments

what if I want the source to be user input?
if you want source to be the yser input - string source = Console.ReadLine(); (providing that you want console input)
"enumerable does not exist in current context"
its in the System.Linq namespace
Ok. This is difficult to translate into my code. I just added the rest of my program to my question. I was trying to make a decorator object for my encryption object, like I did with the L337 decorator object. I need the encrypt for character shift to inherit from text encryption.
0

A naive implementation not using Linq to play around with in unity right away

using System.Collections.Generic;
using UnityEngine;

public class Encryption : MonoBehaviour {

    public string testString = "Every lower case vowel gets replaced by the 'next' one.";

    Dictionary<char, char> charMap;
    string result = "";

    void Start () {

        charMap = new Dictionary<char, char>() {
            {'a','e'},
            {'e','i'},
            {'i','o'},
            {'o','u'},
            {'u','a'}
        };
        result = "";    

        for (int i = 0; i < testString.Length; i++) {

            result += charMap.ContainsKey(testString[i]) ? charMap[testString[i]] : testString[i];
        }

        Debug.Log(result);
    }

}

4 Comments

What if I don't want to use a test string, and I want to use what the user types into an inputfield in unity?
well in your case it would be the return value of _encryption.Encrypt().
So the line would go 'for (int i = 0; i < _Encryption.Encrypt.Length; i++)' ?
try it, its not like your computer will blow up if its not, the worst thing that could happen is that you get an error and its not working (it should be _encryption.Encrypt().Length though). also 2 things. first, a combination of the above answer by dmitry and mine would work for L337Encryption aswell. and second, please tick answers that helped you as correct. oh and a third, my usage of string+string is suboptimal and creates a lot of garbage, read into StringBuilder class of System.Text, thats better for creating strings at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.