Characters replacement in a string in c#

Răspuns Characters replacement in a string in c#

  • 16 aprilie 2012 05:14
     
     

    I have a string and i want to replace its every character with different one. i have a predefined pattern according to which i have to make the conversion.

    'a' must be replaced with 'y', 'b' must b replaced by 'd', 'y' must be replaced by 'a' and so on...

    according to my conversion if a string contains 'aby' then it must be converted to 'yda'.

    How to do that ?

Toate mesajele

  • 16 aprilie 2012 06:12
     
     Răspuns Are cod

    Take a Dictionary<char, char> where you strore all the character mapping. Then, whenver you want to convert a string, use this dictionary to replace characters. For example,

    class Encrypter
    {
        Dictionary<char, char> mappings = new Dictionary<char, char>();
        public Encrypter()
        {
            mappings.Add('a', 'y');
            mappings.Add('b', 'd');
            mappings.Add('c', 'r');
            mappings.Add('r', 'i');
            mappings.Add('k', 'l');
            mappings.Add('e', 's');
            mappings.Add('i', 'm');
            mappings.Add('t', 'o');
        }
        public string ConvertString(string input)
        {
            string output = String.Empty;
            foreach (var item in input)
            {
                output += mappings[item];
            }
            return output;
        }
    }

    Now, you can convert your string like below,

    Encrypter e = new Encrypter();  //-- create instance of above class
    string encryptedString = e.ConvertString("cricket"); //-- Convert the string
    Console.WriteLine(encryptedString);
    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

  • 16 aprilie 2012 07:37
     
      Are cod

    Here you go:

        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Input string");
                string input = Console.ReadLine();
                string newString = "";
    
                foreach (char letter in input)
                {
                    newString += ConvertedChar(letter);
                }
    
                Console.WriteLine(newString);
    
            }
    
            private static string ConvertedChar(char letter)
            {
                switch (letter)
                {
                    case 'a':
                        return "y";
                    case 'b':
                        return "d";
                    case 'y':
                        return "a";
                    default:
                        return "-";
                }
            }
        }

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

  • 16 aprilie 2012 07:41
     
      Are cod

    You can add this following class in your code.

    here, Class Have Function  "Encrypt", "Decrypt" to encode & decode your char or string

    public class MySecurity
        {
            public static string Encrypt(string str)
            {
                string strReturn = "";
                char[] chArray = str.Trim().ToCharArray();
                Random rnd = new Random();
    
                for (int i = 0; i < chArray.Length; i++)
                {
                    strReturn = strReturn + rnd.Next(1, 9999).ToString() + GetEncryptChar(chArray[i]) + "¶";
                }
                return strReturn.TrimEnd('¶');
            }
            private static char GetEncryptChar(char chr)
            {
                char chrReturn;
                switch (chr)
                {
                    case 'a': chrReturn = 'd'; break;
                    case 'b': chrReturn = 'e'; break;
                    case 'c': chrReturn = 'f'; break;
                    case 'd': chrReturn = 'g'; break;
                    case 'e': chrReturn = 'h'; break;
                    case 'f': chrReturn = 'i'; break;
                    case 'g': chrReturn = 'j'; break;
                    case 'h': chrReturn = 'k'; break;
                    case 'i': chrReturn = 'l'; break;
                    case 'j': chrReturn = 'm'; break;
                    case 'k': chrReturn = 'n'; break;
                    case 'l': chrReturn = 'o'; break;
                    case 'm': chrReturn = 'p'; break;
                    case 'n': chrReturn = 'q'; break;
                    case 'o': chrReturn = 'r'; break;
                    case 'p': chrReturn = 's'; break;
                    case 'q': chrReturn = 't'; break;
                    case 'r': chrReturn = 'u'; break;
                    case 's': chrReturn = 'v'; break;
                    case 't': chrReturn = 'w'; break;
                    case 'u': chrReturn = 'x'; break;
                    case 'v': chrReturn = 'y'; break;
                    case 'w': chrReturn = 'z'; break;
                    case 'x': chrReturn = 'a'; break;
                    case 'y': chrReturn = 'b'; break;
                    case 'z': chrReturn = 'c'; break;
                    case 'A': chrReturn = 'D'; break;
                    case 'B': chrReturn = 'E'; break;
                    case 'C': chrReturn = 'F'; break;
                    case 'D': chrReturn = 'G'; break;
                    case 'E': chrReturn = 'H'; break;
                    case 'F': chrReturn = 'I'; break;
                    case 'G': chrReturn = 'J'; break;
                    case 'H': chrReturn = 'K'; break;
                    case 'I': chrReturn = 'L'; break;
                    case 'J': chrReturn = 'M'; break;
                    case 'K': chrReturn = 'N'; break;
                    case 'L': chrReturn = 'O'; break;
                    case 'M': chrReturn = 'P'; break;
                    case 'N': chrReturn = 'Q'; break;
                    case 'O': chrReturn = 'R'; break;
                    case 'P': chrReturn = 'S'; break;
                    case 'Q': chrReturn = 'T'; break;
                    case 'R': chrReturn = 'U'; break;
                    case 'S': chrReturn = 'V'; break;
                    case 'T': chrReturn = 'W'; break;
                    case 'U': chrReturn = 'X'; break;
                    case 'V': chrReturn = 'Y'; break;
                    case 'W': chrReturn = 'Z'; break;
                    case 'X': chrReturn = 'A'; break;
                    case 'Y': chrReturn = 'B'; break;
                    case 'Z': chrReturn = 'C'; break;
                    case '1': chrReturn = '4'; break;
                    case '2': chrReturn = '5'; break;
                    case '3': chrReturn = '6'; break;
                    case '4': chrReturn = '7'; break;
                    case '5': chrReturn = '8'; break;
                    case '6': chrReturn = '9'; break;
                    case '7': chrReturn = '0'; break;
                    case '8': chrReturn = '1'; break;
                    case '9': chrReturn = '2'; break;
                    case '0': chrReturn = '3'; break;
                    default: chrReturn = chr; break;
                }
                return chrReturn;
            }
            private static char GetDecryptChar(char chr)
            {
                char chrReturn;
                switch (chr)
                {
                    case 'a': chrReturn = 'x'; break;
                    case 'b': chrReturn = 'y'; break;
                    case 'c': chrReturn = 'z'; break;
                    case 'd': chrReturn = 'a'; break;
                    case 'e': chrReturn = 'b'; break;
                    case 'f': chrReturn = 'c'; break;
                    case 'g': chrReturn = 'd'; break;
                    case 'h': chrReturn = 'e'; break;
                    case 'i': chrReturn = 'f'; break;
                    case 'j': chrReturn = 'g'; break;
                    case 'k': chrReturn = 'h'; break;
                    case 'l': chrReturn = 'i'; break;
                    case 'm': chrReturn = 'j'; break;
                    case 'n': chrReturn = 'k'; break;
                    case 'o': chrReturn = 'l'; break;
                    case 'p': chrReturn = 'm'; break;
                    case 'q': chrReturn = 'n'; break;
                    case 'r': chrReturn = 'o'; break;
                    case 's': chrReturn = 'p'; break;
                    case 't': chrReturn = 'q'; break;
                    case 'u': chrReturn = 'r'; break;
                    case 'v': chrReturn = 's'; break;
                    case 'w': chrReturn = 't'; break;
                    case 'x': chrReturn = 'u'; break;
                    case 'y': chrReturn = 'v'; break;
                    case 'z': chrReturn = 'w'; break;
                    case 'A': chrReturn = 'X'; break;
                    case 'B': chrReturn = 'Y'; break;
                    case 'C': chrReturn = 'Z'; break;
                    case 'D': chrReturn = 'A'; break;
                    case 'E': chrReturn = 'B'; break;
                    case 'F': chrReturn = 'C'; break;
                    case 'G': chrReturn = 'D'; break;
                    case 'H': chrReturn = 'E'; break;
                    case 'I': chrReturn = 'F'; break;
                    case 'J': chrReturn = 'G'; break;
                    case 'K': chrReturn = 'H'; break;
                    case 'L': chrReturn = 'I'; break;
                    case 'M': chrReturn = 'J'; break;
                    case 'N': chrReturn = 'K'; break;
                    case 'O': chrReturn = 'L'; break;
                    case 'P': chrReturn = 'M'; break;
                    case 'Q': chrReturn = 'N'; break;
                    case 'R': chrReturn = 'O'; break;
                    case 'S': chrReturn = 'P'; break;
                    case 'T': chrReturn = 'Q'; break;
                    case 'U': chrReturn = 'R'; break;
                    case 'V': chrReturn = 'S'; break;
                    case 'W': chrReturn = 'T'; break;
                    case 'X': chrReturn = 'U'; break;
                    case 'Y': chrReturn = 'V'; break;
                    case 'Z': chrReturn = 'W'; break;
                    case '1': chrReturn = '8'; break;
                    case '2': chrReturn = '9'; break;
                    case '3': chrReturn = '0'; break;
                    case '4': chrReturn = '1'; break;
                    case '5': chrReturn = '2'; break;
                    case '6': chrReturn = '3'; break;
                    case '7': chrReturn = '4'; break;
                    case '8': chrReturn = '5'; break;
                    case '9': chrReturn = '6'; break;
                    case '0': chrReturn = '7'; break;
                    default: chrReturn = chr; break;
                }
                return chrReturn;
            }
            public static string Decrypt(string str)
            {
                string strReturn = "";
                string[] strArray = str.Split('¶');
    
                for (int i = 0; i < strArray.Length; i++)
                {
                    strReturn = strReturn + GetDecryptChar(Convert.ToChar(strArray[i].Substring(strArray[i].Length - 1)));
                }
    
    
                return strReturn;
            }
    }

  • 16 aprilie 2012 08:27
     
     

    If you are also interested in a short LINQ solution, then try this:

    string old_characters = "aby"// ... and so on -- the characters to be replaced

    string new_characters = "yda"// ... and so on -- the new characters

     

    string sample = "test: ybbabay";

     

    string result =

             new string( ( from c in sample

                           let k = old_characters.IndexOfc )

                           select k < 0 ? c : new_characters[k] ).ToArray() );

  • 16 aprilie 2012 17:37
     
     
  • 16 aprilie 2012 19:29
     
     
    Pretty much every answer here has uses basic string concatenation to add each character to a temp string before returning it.  This is terrible for performance.  You should use a StringBuilder to build the string and then use .ToString on it when you want the final result.  This will save a LOT of memory, and also a decent amount of processor time, especially if the strings aren't really small.