0

I need to replace every 'a' char with 'b' char, every 'b' char with 'c' char and so on. I've tried to make this with if but code is very very long for every char. Exists a method how mo make this without if or switch or something like that?

        char[] chars = new char[inputString.Length];
        for (int i = 0; i < inputString.Length; i++)
        {
            if (inputString[i] == 'a')
                chars[i] = 'b';
            else if (inputString[i] == 'b')
                chars[i] = 'c';
            else if (inputString[i] == 'c')
                chars[i] = 'd';
            else if (inputString[i] == 'd')
                chars[i] = 'e';
            else if (inputString[i] == 'e')
                chars[i] = 'f';
            else if (inputString[i] == 'f')
                chars[i] = 'g';
            else if (inputString[i] == 'g')
                chars[i] = 'h';
            else
            {
                chars[i] = inputString[i];
            }
        }
        string outputString = new string(chars);

This is how i need to replace:

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

Yamato
  • 17
  • 5
  • Possible consider a `Dictionary` for "key maps to value"? – Jon Skeet May 25 '19 at 15:43
  • 1
    What do you want to happen with 'z'? What about numbers and punctuation? And 'A' to 'Z'? – Andrew Morton May 25 '19 at 15:45
  • Cast it to `int` +1 and back to `char`? – Christian Gollhardt May 25 '19 at 15:49
  • Side note: Take utmost caution when attempting these kind of multiple replacements so that your program logic doesn't accidentally get into a situation where a replacment replaces a prior replacement (unless for whatever strange reason that would be desired). For example, say you have two replacement rules `o`->`u` and `u`->`i`. Applying these two rules in a sequential/iterative manner to the word "shot" might not yield "shut" but rather "sh*t" if you don't pay attention to these things... –  May 25 '19 at 15:50
  • @elgonzo "pock" would have been a better choice, some people for whom English is not their first language might wonder where the asterisk came from. – Andrew Morton May 25 '19 at 16:03
  • @AndrewMorton, i think most people will get the word i self-censored there (i hope). To read anything here on StackOverflow, basic English reading comprehension skills are required anyway... ;-) –  May 25 '19 at 16:10
  • Why not using replace with a temp var? – ErTR May 25 '19 at 16:24
  • @AndrewMorton To complete the question this is a table and an example about my exercise i will edit the post – Yamato May 25 '19 at 17:09
  • @Yamato As the question has been marked as a duplicate, no-one can add another answer. I suggest that you look at the "other way" part of [Rufus L's answer](https://stackoverflow.com/a/55859726/1115360). – Andrew Morton May 25 '19 at 17:13

2 Answers2

0

You could cast it to int and add 1:

char[] chars = new char[inputString.Length];
for (int i = 0; i < inputString.Length; i++)
{
    chars[i] = (char)(inputString[i] + 1);
}
string outputString = new string(chars);

This works, because every char has a numeric representation. However you probably need some extra logic for z or so (depending if you want to constraint the values to the alphabet).

Also since this is tagged with : Use Cryptography if you want to do encryption, instead of rolling your own. Even security experts have problems with creating a secure one.

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • Thank you. I used crypt tag because i have an exercise that tells me to make a "crypt" algorithm for encrypt a string. – Yamato May 25 '19 at 16:23
  • If it's an exercise, than probably your teacher want's you to to some replacement, as above. **But disclaimer** In reality **never never ever** do this, and use battle tested Cryptography Libraries. *(That's the part I hate about our schools. Nothing wrong with some exercise, but calling this encryption is crazy)* – Christian Gollhardt May 25 '19 at 16:26
  • You are right ! – Yamato May 25 '19 at 16:39
  • https://en.wikipedia.org/wiki/Caesar_cipher (which is what question is about) is considered encryption and generally part of cryptography field... Note that [tag:crypt] (which absolutely does not apply to this question) is not about [tag:cryptography] (which I think would be perfectly fine). – Alexei Levenkov May 25 '19 at 16:40
  • [encryption is the process of encoding a message or information in such a way that **only authorized parties can access it**](https://en.wikipedia.org/wiki/Encryption). I can't see this applied in this example. @AlexeiLevenkov – Christian Gollhardt May 25 '19 at 16:43
  • 1
    @ChristianGollhardt :) so RSA is not encryption by that definition as it is theoretically possible to find the key and hence information is available to anyone anyway... or RSA is not encryption because keys can be too small and be actually broken in practice... I understand what you trying to recommend... just disagree with narrowing down definition of "encryption" to "uses strong crypto algorithms" – Alexei Levenkov May 25 '19 at 16:57
0

you can do it this way

    string abc = "abcdefghijklmnopqrstuvwxyz";
    string bcd = new string(abc.Select(x => x == 'z' ? 'a' : (char)(x + 1)).ToArray());
jonaChaz
  • 301
  • 3
  • 17