unregistered
unregistered

Reputation: 21

C# Adding and subtracting to characters in a string

I'm reading from a text file and trying to replace all characters with new ones. Depending on what character I read I want to add 13 to it or subtract 13. My problem is that once I change 'a' to 'n' it changes back to 'a' when I get to 'n' and try to subtract. For ex all 'a' should be 'n' and all 'n' should be 'a'.

while ((inValue = inFile.ReadLine()) != null) {
    for (int letter = 'A'; letter < 'z'; letter++)
    {
        if (letter >= 'A' && letter <= 'M')
            inValue = inValue.Replace((char)letter, (char)    (letter + 13));
        else if (letter >= 'N' && letter <= 'Z')
            inValue = inValue.Replace((char)letter, (char)(letter - 13));
        else if (letter >= 'a' && letter <= 'm')
            inValue = inValue.Replace((char)letter, (char)(letter + 13));
        else if (letter >= 'n' && letter <= 'z')
            inValue = inValue.Replace((char)letter, (char)(letter - 13));
    }
}

Upvotes: 2

Views: 4162

Answers (2)

aguertin
aguertin

Reputation: 494

It's because you are changing them while you are still inside of your for loop. Keep track of the index of each char that needs to be replaced and what needs to go there. After the loop ends, proceed to loop over a Dictionary which will contain

int: index; char: replacementChar.

So something along these lines:

for (int letter = 'A'; letter < 'z'; letter++)
{
    var charTracker = new Dictionary<int, char>();
    char replacement;
    if (letter >= 'A' && letter <= 'M')
        replacement = (char)(letter-13);
    else if (letter >= 'N' && letter <= 'Z')
       replacement = (char)(letter-13));
    else if (letter >= 'a' && letter <= 'm')
       replacement = (char)(letter + 13));
    else if (letter >= 'n' && letter <= 'z')
       replacement = (char)(letter - 13));

    charTracker.Add(letter, replacement);
}

for(var i = 0; i < charTracker.Length -1; i++) 
{
    // Logic to replace chars here
} 

Upvotes: 0

John D
John D

Reputation: 1637

You need to keep track of which character in your string has been changed so you don't change it again. Easier to step through each character in the input string and change it - that way you know it only gets changed once.

while ((inValue = inFile.ReadLine()) != null) {
    // convert to StringBuilder because you can't change the characters within a string
    var sb = new StringBuilder(inValue); 
    for (int i = 0; i < inValue.Length; i++ )
    {
        char letter = sb[i];
        if (letter >= 'A' && letter <= 'M')
            sb[i] = (char)(letter + 13);

        else if (letter >= 'N' && letter <= 'Z')
            sb[i] = (char)(letter - 13);

        else if (letter >= 'a' && letter <= 'm')
            sb[i] = (char)(letter + 13);

        else if (letter >= 'n' && letter <= 'z')
            sb[i] = (char)(letter - 13);
        }
    }
    inValue = sb.ToString(); // Convert back to string
    // ... ... ... do something with `inValue`
}

Upvotes: 2

Related Questions