Click here to Skip to main content
15,894,330 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to encrypt text from a file using rot13. Can someone help me with the code I will need to translate the text?

What I have tried:

C#
private void rotEncrypt() {
      StringBuilder result = new StringBuilder();
      foreach (char i in text.ToString()) {
          if (i >= 'a' && i <= 'm' || i >= 'A' && i <= 'M') {
              result.Append((char)((int)i + 13));
          }
          else if (i >= 'n' && i <= 'z' || i >= 'N' && i <= 'Z') {
              result.Append((char)((int)i - 13));
          }
          else {
              // Leave other characters unchanged
              result.Append(i);
          }

          // Output the resulting string
          lbxEncryption.Items.Add(result.ToString());
Posted
Updated 12-Nov-16 16:44pm
Comments
Patrice T 12-Nov-16 22:00pm    
Describe tour problem or question !
What is wrong in this code ?
Member 12836145 12-Nov-16 22:09pm    
It adds each new character on seperate lines to what was listed above it. Also does not include spaces between words. I have a paragraph I want encrypted to ROF13 in same formatting. How do I fix that?
Patrice T 12-Nov-16 22:17pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Philippe Mori 14-Nov-16 15:21pm    
Your question is not clear. Show us what you expect and what you get.

1 solution

This code is not autonomous, we have no way to test it.
Use the debugger to make sure the problem is here.
Advice: don't mess with casts
C#
result.Append(i + 13);

is enough.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900