Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CP
- {
- internal class MyString
- {
- // initialize the lowercase and uppercase english alphabets
- 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' };
- 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' };
- #region(Fields)
- private readonly char[] ourField;
- #endregion
- #region(Properties)
- public int Length
- {
- // https://www.w3schools.com/cs/cs_properties.php
- // Fields can be made read-only (if you only use the get method), or write-only (if you only use the set method)
- get
- {
- return ourField.Length;
- }
- }
- #endregion
- #region(Indexer)
- public char this[int index]
- {
- get
- {
- return ourField[index];
- }
- }
- #endregion
- #region(Constructors)
- public MyString()
- {
- ourField = new char[0];
- }
- public MyString(char[] value)
- {
- ourField = value;
- }
- public MyString(string value)
- {
- ourField = value.ToCharArray();
- }
- public MyString(char value)
- {
- char f = value;
- char[] fArray = new char[1];
- fArray[0] = f;
- ourField = fArray;
- }
- #endregion
- #region(Implicit convertors)
- // Implicit operator – string to MyString /4
- public static implicit operator MyString(string value)
- {
- return new MyString(value);
- }
- public override string ToString()
- {
- return new string(ourField);
- }
- // Implicit operator – MyString to string
- public static implicit operator string(MyString value)
- {
- return value.ToString();
- }
- #endregion
- #region(Methods)
- public char[] ToCharArray()
- {
- return ourField;
- }
- public MyString ToLower()
- {
- char[] lowerString = new char[ourField.Length];
- for (int i = 0; i < ourField.Length; i++)
- {
- int index = Array.IndexOf(uppercaseAlphabets, ourField[i]);
- if (index == -1)
- {
- lowerString[i] = ourField[i];
- }
- else
- {
- lowerString[i] = lowercaseAlphabets[index];
- }
- }
- return new MyString(lowerString);
- }
- public MyString ToUpper()
- {
- char[] upperMyString = new char[ourField.Length];
- for (int i = 0; i < ourField.Length; i++)
- {
- int index = Array.IndexOf(lowercaseAlphabets, ourField[i]);
- upperMyString[i] = index == -1 ? ourField[i] : uppercaseAlphabets[index];
- }
- return new MyString(upperMyString);
- }
- // override object.Equals
- // public bool Equals(MyString obj)
- // {
- // // Compare character by character of the itself
- // // step 1: we do two char arrays (use our to char array method) for both variables
- // // step 2:
- // // if the length of both arrays (or variables) are not the same
- // // return false
- // // else
- // if (ourField.Length != obj.ourField.Length)
- // {
- // return false;
- // }
- // // if loop through the first and compare values at each index and they all match
- // // return true
- // // else
- // // return false
- // return this.ourField == obj.ourField;
- // }
- public override bool Equals(object obj)
- {
- if (obj == null || GetType() != obj.GetType())
- {
- return false;
- }
- MyString other = (MyString)obj;
- if (Length != other.Length)
- {
- return false;
- }
- for (int i = 0; i < Length; i++)
- {
- if (this[i] != other[i])
- {
- return false;
- }
- }
- return true;
- }
- // override object.GetHashCode
- public override int GetHashCode()
- {
- return ourField.GetHashCode();
- }
- // 'Reverse()' Method
- public MyString Reverse()
- {
- char[] result;
- result = new char[ourField.Length];
- for (int i = 0; i < ourField.Length; i++)
- {
- result[i] = ourField[ourField.Length - 1 - i];
- }
- return new MyString(result);
- }
- #endregion
- #region(Operators)
- public static MyString operator +(MyString a, MyString b)
- {
- int aLength = a.Length;
- int bLength = b.Length;
- char[] result = new char[aLength + bLength];
- for (int i = 0; i < aLength; i++)
- {
- result[i] = a[i];
- }
- for (int i = 0; i < bLength; i++)
- {
- result[aLength + i] = b[i];
- }
- return new MyString(result);
- }
- public static bool operator ==(MyString a, MyString b)
- {
- if (ReferenceEquals(a, b)) // if both are null, or both are same instance
- {
- return true;
- }
- if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
- {
- return false;
- }
- // compare the string values of the MyString instances
- return a.ToString() == b.ToString();
- }
- public static bool operator !=(MyString a, MyString b)
- {
- return !(a == b);
- }
- public static bool operator <(MyString a, MyString b)
- {
- if (a is null || b is null)
- {
- return false;
- }
- int length = Math.Min(a.Length, b.Length);
- for (int i = 0; i < length; i++)
- {
- if (a[i] < b[i])
- {
- return true;
- }
- else if (a[i] > b[i])
- {
- return false;
- }
- }
- return a.Length < b.Length;
- }
- public static bool operator >(MyString a, MyString b)
- {
- if (a is null || b is null)
- {
- return false;
- }
- int length = Math.Min(a.Length, b.Length);
- for (int i = 0; i < length; i++)
- {
- if (a[i] > b[i])
- {
- return true;
- }
- else if (a[i] < b[i])
- {
- return false;
- }
- }
- return a.Length > b.Length;
- }
- public static bool operator >=(MyString a, MyString b)
- {
- return a == b || a > b;
- }
- public static bool operator <=(MyString a, MyString b)
- {
- return a == b || a < b;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement