Advertisement
Placido_GDD

Rot13function

Jun 30th, 2022
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2.  
  3. public class Kata
  4. {
  5.   public static string Rot13(string message)
  6.   {
  7.     string alphabetString =  "abcdefghijklmnopqrstuvwxyz";
  8.     string alphabetStrCaps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  9.     char [] alphabetChar = alphabetString.ToCharArray();
  10.     char [] alphabetCharCaps = alphabetStrCaps.ToCharArray();
  11.     char [] msgCharArray = message.ToCharArray();
  12.     string result = string.Empty;
  13.     int letterIndex = 0;
  14.     int rot13Index = 0;
  15.    
  16.     //Console.Write(message);
  17.     for(int x = 0;x < msgCharArray.Length;x++)
  18.     {  
  19.  
  20.        if(Char.IsLetter(msgCharArray[x]))
  21.        {
  22.           for(int y = 0;y < alphabetChar.Length;y++)
  23.           {
  24.             if(alphabetChar[y] == msgCharArray[x])
  25.             {
  26.               letterIndex = y;
  27.               break;
  28.             }
  29.           }
  30.          if(letterIndex + 13 < 26)
  31.          {
  32.            rot13Index = (letterIndex + 13) ;
  33.            if(Char.IsUpper(msgCharArray[x]))
  34.            {
  35.              msgCharArray[x] = alphabetCharCaps[rot13Index];
  36.              Console.Write( " " + rot13Index.ToString() + " ");
  37.            }
  38.            else
  39.            {
  40.              msgCharArray[x] = alphabetChar[rot13Index];
  41.              Console.Write( " " + rot13Index.ToString() + " ");
  42.  
  43.            }
  44.            
  45.          }
  46.          else if(letterIndex + 13 == 26)
  47.          {
  48.            rot13Index = 26 - 1;
  49.            if(Char.IsUpper(msgCharArray[x]))
  50.            {
  51.              msgCharArray[x] = alphabetCharCaps[rot13Index];
  52.              Console.Write( " " + rot13Index.ToString() + " ");
  53.  
  54.            }
  55.            else
  56.            {
  57.              msgCharArray[x] = alphabetChar[rot13Index];
  58.              Console.Write( " " + rot13Index.ToString() + " ");
  59.      
  60.            }
  61.          }
  62.          else if(letterIndex + 13 > 26)
  63.          {
  64.            rot13Index = ((letterIndex + 13) % alphabetChar.Length);
  65.            if(Char.IsUpper(msgCharArray[x]))
  66.            {
  67.              msgCharArray[x] = alphabetCharCaps[rot13Index];
  68.              Console.Write( " " + rot13Index.ToString() + " ");
  69.  
  70.            }
  71.            else
  72.            {
  73.              msgCharArray[x] = alphabetChar[rot13Index];
  74.              Console.Write( " " + rot13Index.ToString() + " ");
  75.  
  76.            }
  77.          }
  78.        }
  79.     }
  80.    
  81.     result = new string(msgCharArray);
  82.     return result;
  83.   }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement