Advertisement
themaleem

Untitled

Apr 14th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CP
  8. {
  9.   internal class MyString
  10.   {
  11.     // initialize the lowercase and uppercase english alphabets
  12.     private readonly char[] lowercaseAlphabets = { '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' };
  13.     private readonly char[] uppercaseAlphabets = { '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' };
  14.  
  15.     #region(Fields)
  16.     private readonly char[] ourField;
  17.     #endregion
  18.  
  19.     #region(Properties)
  20.     public int Length
  21.     {
  22.       // https://www.w3schools.com/cs/cs_properties.php
  23.       // Fields can be made read-only (if you only use the get method), or write-only (if you only use the set method)
  24.       get
  25.       {
  26.         return ourField.Length;
  27.       }
  28.     }
  29.     #endregion
  30.  
  31.     #region(Indexer)
  32.     public char this[int index]
  33.     {
  34.       get
  35.       {
  36.         return ourField[index];
  37.       }
  38.  
  39.     }
  40.     #endregion
  41.  
  42.     #region(Constructors)
  43.     public MyString()
  44.     {
  45.       ourField = new char[0];
  46.     }
  47.  
  48.     public MyString(char[] value)
  49.     {
  50.       ourField = value;
  51.     }
  52.  
  53.     public MyString(string value)
  54.     {
  55.       ourField = value.ToCharArray();
  56.     }
  57.  
  58.     public MyString(char value)
  59.     {
  60.       char f = value;
  61.       char[] fArray = new char[1];
  62.       fArray[0] = f;
  63.       ourField = fArray;
  64.  
  65.  
  66.     }
  67.     #endregion
  68.  
  69.     #region(Implicit convertors)
  70.     // Implicit operator – string to MyString /4
  71.     public static implicit operator MyString(string value)
  72.     {
  73.       return new MyString(value);
  74.     }
  75.  
  76.     public override string ToString()
  77.     {
  78.       return new string(ourField);
  79.     }
  80.  
  81.     // Implicit operator – MyString to string
  82.     public static implicit operator string(MyString value)
  83.     {
  84.       return value.ToString();
  85.     }
  86.  
  87.     #endregion
  88.  
  89.     #region(Methods)
  90.     public char[] ToCharArray()
  91.     {
  92.       return ourField;
  93.     }
  94.  
  95.     public MyString ToLower()
  96.     {
  97.  
  98.       char[] lowerString = new char[ourField.Length];
  99.  
  100.  
  101.       for (int i = 0; i < ourField.Length; i++)
  102.       {
  103.         int index = Array.IndexOf(uppercaseAlphabets, ourField[i]);
  104.         if (index == -1)
  105.         {
  106.           lowerString[i] = ourField[i];
  107.         }
  108.         else
  109.         {
  110.           lowerString[i] = lowercaseAlphabets[index];
  111.         }
  112.       }
  113.       return new MyString(lowerString);
  114.     }
  115.  
  116.     public MyString ToUpper()
  117.     {
  118.  
  119.       char[] upperMyString = new char[ourField.Length];
  120.       for (int i = 0; i < ourField.Length; i++)
  121.       {
  122.         int index = Array.IndexOf(lowercaseAlphabets, ourField[i]);
  123.         upperMyString[i] = index == -1 ? ourField[i] : uppercaseAlphabets[index];
  124.       }
  125.       return new MyString(upperMyString);
  126.     }
  127.  
  128.  
  129.     // override object.Equals
  130.     // public bool Equals(MyString obj)
  131.     // {
  132.  
  133.     //   // Compare character by character of the itself
  134.     //   // step 1: we do two char arrays (use our to char array method) for both variables
  135.     //   // step 2:
  136.     //   // if the length of both arrays (or variables) are not the same
  137.     //   //    return false
  138.     //   // else
  139.  
  140.     //   if (ourField.Length != obj.ourField.Length)
  141.     //   {
  142.     //     return false;
  143.     //   }
  144.  
  145.     //   //   if loop through the first and compare values at each index and they all match
  146.     //   //      return true
  147.     //   //   else
  148.     //   //     return false
  149.  
  150.  
  151.     //   return this.ourField == obj.ourField;
  152.     // }
  153.  
  154.     public override bool Equals(object obj)
  155.     {
  156.       if (obj == null || GetType() != obj.GetType())
  157.       {
  158.         return false;
  159.       }
  160.  
  161.       MyString other = (MyString)obj;
  162.  
  163.       if (Length != other.Length)
  164.       {
  165.         return false;
  166.       }
  167.  
  168.       for (int i = 0; i < Length; i++)
  169.       {
  170.         if (this[i] != other[i])
  171.         {
  172.           return false;
  173.         }
  174.       }
  175.  
  176.       return true;
  177.     }
  178.  
  179.     // override object.GetHashCode
  180.     public override int GetHashCode()
  181.     {
  182.       return ourField.GetHashCode();
  183.     }
  184.  
  185.     // 'Reverse()' Method
  186.     public MyString Reverse()
  187.     {
  188.       char[] result;
  189.       result = new char[ourField.Length];
  190.  
  191.       for (int i = 0; i < ourField.Length; i++)
  192.       {
  193.         result[i] = ourField[ourField.Length - 1 - i];
  194.       }
  195.  
  196.       return new MyString(result);
  197.     }
  198.     #endregion
  199.  
  200.     #region(Operators)
  201.     public static MyString operator +(MyString a, MyString b)
  202.     {
  203.       int aLength = a.Length;
  204.       int bLength = b.Length;
  205.       char[] result = new char[aLength + bLength];
  206.       for (int i = 0; i < aLength; i++)
  207.       {
  208.         result[i] = a[i];
  209.       }
  210.       for (int i = 0; i < bLength; i++)
  211.       {
  212.         result[aLength + i] = b[i];
  213.       }
  214.       return new MyString(result);
  215.     }
  216.  
  217.     public static bool operator ==(MyString a, MyString b)
  218.     {
  219.       if (ReferenceEquals(a, b)) // if both are null, or both are same instance
  220.       {
  221.         return true;
  222.       }
  223.  
  224.       if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
  225.       {
  226.         return false;
  227.       }
  228.  
  229.       // compare the string values of the MyString instances
  230.       return a.ToString() == b.ToString();
  231.     }
  232.  
  233.     public static bool operator !=(MyString a, MyString b)
  234.     {
  235.       return !(a == b);
  236.     }
  237.  
  238.     public static bool operator <(MyString a, MyString b)
  239.     {
  240.       if (a is null || b is null)
  241.       {
  242.         return false;
  243.       }
  244.  
  245.       int length = Math.Min(a.Length, b.Length);
  246.  
  247.       for (int i = 0; i < length; i++)
  248.       {
  249.         if (a[i] < b[i])
  250.         {
  251.           return true;
  252.         }
  253.         else if (a[i] > b[i])
  254.         {
  255.           return false;
  256.         }
  257.       }
  258.  
  259.       return a.Length < b.Length;
  260.     }
  261.  
  262.     public static bool operator >(MyString a, MyString b)
  263.     {
  264.       if (a is null || b is null)
  265.       {
  266.         return false;
  267.       }
  268.  
  269.       int length = Math.Min(a.Length, b.Length);
  270.  
  271.       for (int i = 0; i < length; i++)
  272.       {
  273.         if (a[i] > b[i])
  274.         {
  275.           return true;
  276.         }
  277.         else if (a[i] < b[i])
  278.         {
  279.           return false;
  280.         }
  281.       }
  282.  
  283.       return a.Length > b.Length;
  284.     }
  285.  
  286.     public static bool operator >=(MyString a, MyString b)
  287.     {
  288.       return a == b || a > b;
  289.     }
  290.  
  291.     public static bool operator <=(MyString a, MyString b)
  292.     {
  293.       return a == b || a < b;
  294.     }
  295.     #endregion
  296.  
  297.   }
  298.  
  299. }
  300.  
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement