C# Console Application for ROT13 (encryption)

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 3262 Views - Last Post: 02 December 2012 - 11:02 AM Rate Topic: -----

#1 rideronix  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 24-November 12

C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 02:46 PM

Hey everyone,

I'm trying to create a console function where you could type any sentence (so only string) and make a ROT13 rotation to that sentence. However, my level in C# is pretty low and I'm having some troubles with that...

I'll explain what I'm trying to do:

1/ Take the array made by the static void main.
2/ Try to convert the entered sentence (so arguments into char).
3/ Take each char and convert it into an integer (to get ASCII number).
4/ Add +13 or -13 depending on the letter to make the rotation.
5/ Put the int into char and assemble the char into a string.
6/ Output the sentence.


I'm blocking for step 2,3 and 5.


static void Main(char[] args)
        {
            for (int n = 0; n < args.Length; n++)
            {
                if (letter >= 'a' && letter <= 'z')
                {
                    if (letter > 'm')
                    {
                        letter -= 13;
                    }
                    else
                    {
                        letter += 13;
                    }
                }
            }




Thanks in advance !

Is This A Good Question/Topic? 0
  • +

Replies To: C# Console Application for ROT13 (encryption)

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,866
  • Joined: 02-June 10

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 03:38 PM

"I'm having trouble" is very vague.

What is your exact question?
Was This Post Helpful? 0
  • +
  • -

#3 rideronix  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 24-November 12

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 03:42 PM

Okay I'll explain deeper my problem.

I don't know how to:
- convert the entered sentence into chars.
- Take each char and convert it into an integer. Perhaps:
 ((int)'a') 

- Put the int into char and assemble the char into a string.

Is this clearer ?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,866
  • Joined: 02-June 10

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 03:52 PM

Quote

- convert the entered sentence into chars.

A sentence is a string. A string is already an array of chars.

"I am yogi bear" is already
[0] = I
[1] = {space}
[2] = a
[3] = m
...




Quote

Take each char and convert it into an integer. Perhaps:
1
((int)'a')

yep. Cast from one to the other.

int aInt = (int)'a';
char aChar = (char)aInt;


A little bit of experimentation would have gone a long way considering you already had the first part of that.
Was This Post Helpful? 0
  • +
  • -

#5 rideronix  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 24-November 12

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 04:23 PM

I'm going to try to separate my code and explain each step because I'm still stuck on something :


1/ The user enters a sentence, for example: "Hey Im George".
static void Main(string[] args)

The sentence is now in the args string array.

2/ We are going to take each char of the elements of the args array to later apply the encryption on them.
So first we set a loop.
for ( int n = 0; n < args.Length; n++)


And now we get each char and this is where I'm stuck.

            {
                Char.TryParse(args[n],out p);
                If (p >= 'a' && p <= 'z')
                   { my algorithm from earlier }
            }


However I get a conflict between string and char because Visual is trying to convert "Hey" into 'Hey' which isn't right... How may I only take first char ?
I know you said it'd be like: [0] = 'H', [1] = 'e', ... but on which array do I do this ? Since I only got the "args" array.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,866
  • Joined: 02-June 10

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 04:45 PM

put a breakpoint at this method.

Look at the runtime value of args.
What are the values of each element of that array, using your example: "Hey I'm George" ?

Also, this code does not appear in your earlier code.
So I have no idea where it came from or what type p is.

{
    Char.TryParse(args[n],out p);
    If (p >= 'a' && p <= 'z')
       { my algorithm from earlier }
}

Was This Post Helpful? 0
  • +
  • -

#7 rideronix  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 24-November 12

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 05:37 PM

Not sure you actually understand my problem. I'm going to do it simpler.

My encryption algorithm: a -> b, b -> c, c -> d, etc...
Example: Hey -> Ifz

How it works: ASCII of a is 97, ASCII of b is 98, etc...

What I want to do: Take each character from a string and convert it to a integer and add +1 to this integer. Then change the integer into a char again and rebuild the string.

Example: Hey
(int)'H' = 105
Add 1
Return 105 = 'I'

(int)'E' = 103
Add 1
Return 104 = 'f'

(int)'y' = 121
Add 1
Return 122 = 'z'

Return 'H'+'e'+'y' = "Hey"



And I'm stuck at: Take each char of a string and make a integer to add 1 to it.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,866
  • Joined: 02-June 10

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 06:44 PM

You've just described exactly what you need. And described it well I might add.

So short of someone writing the code for you I fail to understand what the problem is?

Your code shows you know how to do a for loop.
I showed you how to cast both direction int<->char
You've shown that you know exactly what the logic is.

At least make an effort to do do it then come back with your updated code.
Was This Post Helpful? 0
  • +
  • -

#9 rideronix  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 24-November 12

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 06:58 PM

You are correct I do know most of the code I need to read but let me explain again what is exactly I don't know.


My string is in the "args string array"
I create a loop and for each args[n], n int from 0 to args.Length

However if my sentence is "Hello !"

Args[0] = "Hello" and not 'H'.
How do I make it so that [0] = 'H', [1] = 'e', [2] = 'l' , etc...
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 08:46 PM

You are misreading the function arguments:
void func1(string [] arrayOfStringsParameter)
{
}

void func2(string stringParameter)
{
}



In the case of func1, the parameter is an array of strings. In the case of func2, the parameter is a string.

To access the characters of a string, you would use the [] operator like:
string myString = "Hello";
char firstChar = myString[0];



To access the elements of at array you would also use the [] operator like:
char [] myCharArray = { 'H', 'e', 'l', 'l', 'o' };
char firstChar = myCharArray[0];


Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,866
  • Joined: 02-June 10

Re: C# Console Application for ROT13 (encryption)

Posted 30 November 2012 - 08:51 PM

string someArgs = "Yogi Bear"; // a string is a char[]
foreach (char letter in someArgs)
{
    // do something with the letter
}

Was This Post Helpful? 0
  • +
  • -

#12 rideronix  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 24-November 12

Re: C# Console Application for ROT13 (encryption)

Posted 01 December 2012 - 07:03 AM

So if I enter: "Hey Im Mike"

I need to do :
string someArgs = args[0]

foreach (char letter in args[0]


But then I need to convert the char into an int and :
(int)letter = int.Parse(args[n]);

                if (letter >= 'a' && dechif <= 'z')
                {
                    if (letter > 'm')
                    {
                        letter -= 13;
                    }
                    else
                    {
                        letter += 13;
                    }


And this doesn't work it says I can't convert types...
Was This Post Helpful? 1
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: C# Console Application for ROT13 (encryption)

Posted 01 December 2012 - 07:13 AM

You do not Parse() an character to get its ASCII or Unicode value. You merely cast it.
Was This Post Helpful? 0
  • +
  • -

#14 rideronix  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 24-November 12

Re: C# Console Application for ROT13 (encryption)

Posted 01 December 2012 - 08:21 AM

But when I do :

(int)letter = (int)args[n];


I get a convertion error... :/
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: C# Console Application for ROT13 (encryption)

Posted 01 December 2012 - 08:27 AM

Because args is an array of strings. Note that I said you should cast a character to an int.

Sent from my T-Mobile G2 using Tapatalk 2

This post has been edited by Skydiver: 01 December 2012 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2