Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

C# problem

 
Oduche henry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good day all,
please I would be most grateful if anyone could help me with the code for a C# program that can read ordinary English text from a text file and encode it by replacing each text with the 13th next letter in the alphabet for example "Ang Rus" becomes "Nat Ehf" using the CryptoStream class i.e DES and TripleDES after which the encoded text is saved to a text file which can also be decoded also be decoded back to ordinary English using the same code.Thank you all
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always tell us what your thread is about. No, we do not simply hand out code; show us what you already have and let us see what we think of it.
 
Oduche henry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone
this is what i have been able to do so far, but the problem i am faced with is that the loop does not seem to see all the "if" and else "if" statements it just executes the "else" statement at the end of the loop so all it ends up printing is the "from_file" therefore the text in the original text file "MyFile.txt" is exactly what is rewritten to the new file "MyFile1.txt" please i need help.
this is the source code:

 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What a dreadful bank of "if"s. Are you really adding a constant amount to each char? You realise that at least in Java™, a char is an integer, so you can do arithmetic with it; you can add numbers, use the % operator for remainders, and add the remainder to 'a'. I am not sure whether it works in C#, but you should try it with some simple applications and see what happens.

Beware; if you get 'a' == 1 and add 1 to 'a' you will get 'b', so you may need an additional - 1. You may need to cast the result back to a (char) or use a method of Convert.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well as far as i remember it is possible to arithmetically compute chars in C# too ... so just try that and forget this bunch of if's ...

you could have used switch either buddy
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We seem to agree about arithmetic on chars, but I don't think a switch-case-break block would have been any neater than all those ifs.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also at line 22 you have

so none of those if-statements where you compare from_file to a single character is ever going to be true.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic