|
but for numbers how to make? i need to replace 0 with 9 and so on
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
|
|
|
|
|
Now, you have changed the question.
int offset = '9' + '0';
string ciphered = string.Join("",
inputString.Select(ch =>
(char)
(
Char.IsDigit(ch) ? offset - ch : ch + 1
)
)
); Is this homework ?
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 26-May-19 5:41am.
|
|
|
|
|
Yes, is homework, when i write the question i didn't know that for numbers is different than letters, then i tried and i realised that i don't know how to make this. If you are interested full task is that:
Make a crypt algorithm that replace all letters and number like that:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Z A B C D E F G H I J K L M N O P Q R S T U V W X Y
a b c d e f g h i j k l m n o p q r s t u v w x y z
b c d e f g h i j k l m n o p q r s t u v w x y z a
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
Example:
string: TEXTcsharp#2367
result: SDWSdtibsq#7632
Thank You for Help !
|
|
|
|
|
What I am really interested in is how we can help you start studying, and experimenting, so that you get a firm basis for becoming the programmer you want to be.
In the real world, no one will write code for you, and, teachers, and job interviewers, will understand quickly when you try and explain what code does ... whether you wrote it, or not.
Let's see you complete your assignment by handling the differences in upper- and lower- case character handling.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
You are right, but i don't have a material from school or something like that, this "homework" is a contest from another country. This is the code and it works, thank you for all
int offset = '9' + '0';
string test = string.Join("", normal.Select(ch => (ch >= 'a' && ch < 'z') ? (char)(ch + 1) : (ch > 'A' && ch <= 'Z') ? (char)(ch - 1) : (ch == 'z') ? (char)'a' : (ch == 'A') ? (char)'Z' : ((char)(Char.IsDigit(ch) ? offset - ch : ch + 1))));
|
|
|
|
|
Do you think your code is now "original" enough to submit to a contest as your own work ?
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
But i don't send the code, i make this contest only for understand some things in C#. I'm not able to participate for this contest because is not for my country
|
|
|
|
|
That's good to know, and I think posing "code challenges" to yourself is an excellent way to learn !
I have found my students seem to understand Linq more quickly if I explain 'Select as meaning "transform each element, and return a new IEnumerable collection" ... and, 'Where as meaning "select."
Your code:
int offset = '9' + '0';
string test = string.Join("", normal.Select(ch => (ch >= 'a' && ch < 'z') ? (char)(ch + 1) : (ch > 'A' && ch <= 'Z') ? (char)(ch - 1) : (ch == 'z') ? (char)'a' : (ch == 'A') ? (char)'Z' : ((char)(Char.IsDigit(ch) ? offset - ch : ch + 1)))); While this is fine as "proof of concept," I'd ask you to imagine a programming job interview where you submitted your code, or this:
public string Encode(string source)
{
StringBuilder sb = new StringBuilder();
int offset = '9' + '0';
foreach (char ch in source)
{
if (Char.IsDigit(ch))
{
sb.Append((char) (offset - ch));
continue;
}
if (Char.IsUpper(ch))
{
if (ch == 'Z')
{
sb.Append('Y');
}
else
{
sb.Append((char) (ch - 1));
}
continue;
}
if (ch == 'z')
{
sb.Append('a');
}
else
{
sb.Append((char) (ch + 1));
}
}
return sb.ToString();
} Which version do you think more likely to get you a job ? Which version do you think you could more easily change if the encoding requirements changed ? Which version do you think another programmer could more easily understand ?
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
this is the first time when i use linq expresions and i like more than second version, i think that are more intuitive. About changes, i think that is easier to change second version because all code is more spaceful, i'm sure you understand what i mean. And the answer for third question, i think that is important how another programmer start, if he start with linq it will be easier for him to understand linq and if he start using functions without linq expressions is easier to understand second versions. I like both methods and i think that is important to know many methods for solving a task
|
|
|
|
|
C# 8 will allow you to use expression bodies that return values in a 'switch statement; I am not yet comfortable with this very different style:
int offset = '9' + '0';
public char EncodeChar(char ch)
{
return ch switch
{
Char c when Char.IsDigit(ch) =>
((char) (offset - ch)),
Char c when Char.IsUpper(ch) =>
ch == 'Z' ? 'Y' : ((char)(ch - 1)),
Char c =>
ch == 'z' ? 'a' : ((char) (ch + 1))
};
}
public string Encode3(string source)
{
StringBuilder sb = new StringBuilder();
foreach (char ch in source)
{
sb.Append(EncodeChar(ch));
}
return sb.ToString();
}
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
very very nice, could you tell me a way to learn c#? i make challanges but i don't know many basics of c#, for example one year ago i made a program with incapsulation, but i didn't know name of method, after many months i found that it is a basic OOP method and so on. I don't know, in my country i don't have teachers for C#, and it is very hard for me to learn.
|
|
|
|
|
One of my old friends, Mihai Chis [^], is a world-class C# programmer in Bucharest, and I bet there are other resources. In Bulgaria, a group of C# programmers published an excellent, free, guide to programming in C# (English available): [^].
You can search CP for recommendations on other books by several people, including me: [^]
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Thank You !
|
|
|
|
|
if you want to continue the discussion, keep in touch add me on skype: yamato yammy
|
|
|
|
|
I guess some regex guru could come up with a solution far less readable than this one!
|
|
|
|
|
That's why I suggested translation arrays - you can add the characters you want and the code doesn't change:
private const string transA = "abcdefghijklmnopqurstuvwxyz0123456789";
private const string transB = "bcdefghijklmnopqurstuvwxyza9876543210";
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
oh, now i understand what this trans do, thank you. It works with this method, I will try the other method with select and I will put it here
|
|
|
|
|
And you use the same code but with transA and transB swapped over to decode it.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Now that the OP is slowly revealing more and more complexity in the substitution rules ... this approach makes more and more sense to me
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
I learned many years ago that the only constant in this profession is that everything changes!
So I always try to code for alterations, by using the most flexible solution available.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Okay then, I won't mention Heraclitus: "panta rhei"
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Quote: τὰ πάντα ῥεῖ καὶ οὐδὲν μένει I didn't say it was an original philosophy, just an OriginalGriff philosophy!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I didn't say it was UnOriginalGriff
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 27-May-19 6:48am.
|
|
|
|
|
Constants ain't!
Variables won't!
... That is an old truth from at least 30 years ago. I haven't been thinking of it for years (or heard others refer to it). Maybe I should have a t-shirt made with that text.
|
|
|
|
|
Many. many years ago I worked with a FORTRAN compiler that passed by reference. So you could do the equivalent of this:
void AddTen(int val)
{
val += 10;
}
...
x = 666;
AddTen(x);
Console.WriteLine($"{x} == {666}?");
x = 666;
AddTen(666);
Console.WriteLine($"{x} == {666}?");
x = 666;
Console.WriteLine($"{x} == {666}?"); And you would get the output
676 == 666?
666 == 676?
676 == 676? As the constant value had been changed for ever more ...
Debugging was a mans game back then!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|