C# for-loop, the loops run more then it should!? Kinda

Okey here is the code:
(I used a set value for int key(= 143) when i tried to work out the problem.
First time i use the code, i use it to encrypt the string t = (admin lösenord), int key(143)
The second time, when i want to decrypt it; string t = (admin lösenord), int -key(143)

    public static string Encrypt(string t, int key)
    {
        char ch;
        string temp = "";
        for (int i=0; i < 17-1; i++)
        {
            ch = Convert.ToChar(
                t.Substring(i, 1));
            ch = (char) (ch + key);
            temp += ch;
        }
        return temp;

This code actually works, but its only usfull for this string. I mean its length.
A longer or shorter string “length” will not work.
The problem was: for (int i=0; i < t.Length-1; i++)

It looped throu the block, change every letter as its supposed to do, one at the time. But then at the end, it loop 2 extra times and when i printed out the decryption, the outcome was: admin lösenordet?? (instead of: admin lösenordet)
So i tried out some different things, to try get rid of the two extra ‘??’, with no luck!
Change stuff like: “for (int i=0; i <= t.Length-1; i++)” “for (int i=0; i < t.Length-2; i++)” “for (int i=0; i <= t.Length-2; i++)”
Nothing help, so then i just tried “for (int i=0; i < 17h-1; i++)” and it works, unfortunately that just made the code unusful for other operations too.

PS at the begining i also had a error infront of the text, i had ‘?’ before the “admin lösenordet??” too, like this:
“?admin lösenordet??”.
I finally saw the problem and it was that i had initiated the string temp, like this = " "; and that some how added, not a spacemark, more like a null some how. Because i got a spacemark between “admin lösenordet” and it can handle that…

Best regards

The correct condition for your for loop should be:

i < t.Length

e.g.
If "t" were the string "Hello", "t.Length" would be 6.
So, the values of "i" in that for loop would iterate through (0, 1, 2, 3, 4, 5), which coveres every index in the string "Hello".

Or, if "t" were the string "admin lösenordet", "t.Length" would be 16...
...and that looks familliar - because it's the value you said works for that value of t? "17-1"?

why dont you try assigning the String.length to an foo int, take away one from it and use that for your for loop

public static string Encrypt(string t, int key)
{
char ch;
string temp = "";
string cp = t;
int foo = t.length();
foo --;
for (int i=0; i {
//stuff here
}

Hello, ive tried that before(did it again, now that you write that), the outcome is:
Admin lösenordet?

Hmm okey, never done that before. How should the for loop look like then?
Its funny the school book i have for this course, there example dont have this problem.. hehe

LOL forget about that "string cp = t;", didnt know it was still there when i created this thread, that was just rageguy in me tryingAnd/naming variables..
I removed it now, so the code now is:

public static string Encrypt(string t, int key)
{
char ch;
string temp = "";
for (int i=0; i < t.Length -1; i++)
{
ch = Convert.ToChar(
t.Substring(i, 1));
ch = (char) (ch + key);
temp += ch;
}
return temp;

always remeber that a String is alot like a charecter array, the first charecter is at the index of 0.

If i have a foo string = hello world; , the method foo.length() would return 10, because the index started at 0 and went up to 10.

1 Like

Iam not sure we/i understand eachother here. I forgot to say i did step in to it with the debugger.
And while it encrypt the text, it just change/convert the chars in the string. Nothing more/nothing less, while using t.Length.
Its only when i try to decrypt it, that this happens.
@jonnyp3f

What's your function for decryption?

I use the same code, for both encrypt/decrypt. The only differents is, at crypt i use the key value and when decrypt i send the key like "-key", in other words i subtract the key value.

Best regards! @SpaceCat

@Zemos
Neater way of doing it

        public static string Encrypt(string t, int key)
        {
            StringBuilder builder = new StringBuilder();
            char ch;

            foreach (char c in t)
            {
                ch = (char)(c + key);
                builder.Append(ch);
            }
            return builder.ToString();
        }
1 Like

@GoingPostal13 @SpaceCat @jonnyp3f

Okey, found out the problem to day. Alot of work and i got support from my teatcher. And we finally figured it out.
It was so simple LOL

But it was in another class/method, where the problem was, that is handle the way the text was written to the different files.

THIS CODE MADE IT WORK:
public static void Write(string t, string filnamn)
{
StreamWriter fileForWrite = new StreamWriter(filnamn);
fileForWrite.Write(t);
fileForWrite.Close();
}

THIS CODE(my original)DOSENT WORK:
public static void Write(string t, string filnamn)
{
StreamWriter fileForWrite = new StreamWriter(filnamn);
fileForWrite.WriteLine(t);
fileForWrite.Close();
}

EDIT this was hard for me to explain with my english hehe
Lets try again; the save to file method adds a newline(\n) after the "endline".
So when its gets decrypted, it decrypt that extra line too..........

Best regards!

PS Gonna check(use that prob.) that out @GoingPostal13