0

I'm trying to create a little project (I'm new to C#) where someone can input a key and a string to encrypt. However I have a logic error and it does not display. I have tried my best to explain each line best I could.

    char[] alphabet = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); // so i dont have to make each thig in the array

    Console.WriteLine("KeY: "); // ask for key
    string key1 = Console.ReadLine(); // store it in a variabe
    int key = Convert.ToInt32(key1); // make it a number


    Console.WriteLine("Code to encrypt: ");

    string code = Console.ReadLine(); // strig that need to be encryot

    

    string EncryptedString = ""; // blank string to insert encrypted letter into



    int leng = code.Length;

    int i = 0;
    while (i > leng)
    {

        char x = code[i]; // x is after every loop the consequetive letter in the string that needs encrypting

        

        int y = Array.IndexOf(alphabet, x); // finds the index position that "x" is in the alphabet, e.g (a = 0, b = 1)

        int z = alphabet[y + key]; // z is the letter index of the letter in alphabet but with the key amount over , imagine the key was 2, e.g (a = c)

        string w = Convert.ToString(z); // w is z converted to a string

        EncryptedString.Insert(i - 1, w); // is the the position you want to insert the letter, w is the letter you want inserting
       



       
        
        i++;

    }

Console.WriteLine(EncryptedString);

a logic error somewhere and the encrypted string just won't display

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • .NET strings are immutable. `EncryptedString.Insert` doesn't do what you think it does; it returns a new string (which your code discards). `StringBuilder` is the .NET concept of a mutable string. – Jeroen Mostert Apr 25 '23 at 20:33
  • You’re also missing the part where you loop back to ‘a’ for ‘z + 1’, not to mention what happens if the key was say 123 – stuartd Apr 25 '23 at 22:10

1 Answers1

0

First change from string to CharArray

char[] codeArray = code.toCharArray();
int leng = codeArray.Length;

int i = 0;
while (i > leng)
{

    char x = codeArray[i]; // x is after every loop the consequetive letter in the string that needs encrypting

try this and post the results

  • _try this and post the results_ - that’s not how answers work.. – stuartd Apr 25 '23 at 20:52
  • the sign of your while is changed, change the sign. while (i < leng) You can also replace EncryptedString.Insert() with : EncryptedString += w; – Arthur Correa Apr 25 '23 at 21:25
  • another tip, do not use while in these situations, use for for (int i=0; i < leng; i++) { your code for loop } – Arthur Correa Apr 25 '23 at 21:29
  • From what I understand you want to change the type of the variable z also from int to char, otherwise the final result will be a string of numbers with the character codes and not that 'imagine the key was 2, e.g (a = c)' , then use char z = alphabet[y + key]; – Arthur Correa Apr 25 '23 at 21:53