Пример #1
0
        //3метод
        public Mystring Cut(int num, int count) //вырезать из строки символы начиная с num количеством count - 1 ПЕРЕГРУЗКА
        {
            Mystring chstr = new Mystring();

            if (num + count <= Text.Length)
            {
                Mystring Text_tmp = new Mystring(Text.Length - count);
                for (int i = 0; i < num; i++)
                {
                    Text_tmp.Text[i] = Text[i];
                }

                for (int i = num + count; i < Text.Length; i++)
                {
                    Text_tmp.Text[i - count] = Text[i];
                }

                Text = Text_tmp.Text;
            }
            for (int i = 0; i < Text.Length; i++)
            {
                chstr[i] = Text[i];
            }
            return(chstr);
        }
Пример #2
0
        public int Index(Mystring s2, int num) //поиск в строке  подстроки s2 начиная с элемента номер num - 2 ПЕРЕГРУЗКА
        {
            int findindex = -1;

            if (s2.Length + num <= Text.Length)
            {
                if (Text.Length >= s2.Length)
                {
                    for (int i = num; i < Text.Length - s2.Length + 1; i++)
                    {
                        for (int j = 0; j < s2.Length; j++)
                        {
                            if (Text[i + j] != s2.Text[j])
                            {
                                break;
                            }
                            if (j == s2.Length - 1)
                            {
                                findindex = i;
                            }
                        }
                        if (findindex > -1)
                        {
                            break;
                        }
                    }
                }
            }
            return(findindex);
        }
Пример #3
0
        //2 метод
        public int Index(Mystring s2) //поиск в строке  подстроки s2 - 1 ПЕРЕГРУЗКА
        {
            int findindex = -1;

            if (Text.Length >= s2.Length)
            {
                for (int i = 0; i < Text.Length - s2.Length + 1; i++)
                {
                    for (int j = 0; j < s2.Length; j++)
                    {
                        if (Text[i + j] != s2.Text[j])
                        {
                            break;
                        }
                        if (j == s2.Length - 1)
                        {
                            findindex = i;
                        }
                    }
                    if (findindex > -1)
                    {
                        break;
                    }
                }
            }
            return(findindex);
        }
Пример #4
0
        public Mystring Cut(Mystring s2) //вырезать из строки все вхождения подстроки s2 - 2 ПЕРЕГРУЗКА
        {
            Mystring chstr = new Mystring();

            if (s2.Length <= Text.Length)
            {
                Mystring Text_tmp = new Mystring();
                Mystring Text_tmp2 = new Mystring();
                int      findindex = 1, n = 0;
                while (findindex != -1)
                {
                    findindex = -1;
                    if (Text.Length >= s2.Length)
                    {
                        for (int i = n; i < Text.Length - s2.Length + 1; i++)
                        {
                            for (int j = 0; j < s2.Length; j++)
                            {
                                if (Text[i + j] != s2.Text[j])
                                {
                                    break;
                                }
                                if (j == s2.Length - 1)
                                {
                                    findindex = i;
                                }
                            }
                            if (findindex > -1)
                            {
                                break;
                            }
                        }
                    }
                    if (findindex > -1)
                    {
                        Text_tmp2 = new Mystring(findindex);
                        for (int i = 0; i < findindex; i++)
                        {
                            Text_tmp2.Text[i] = Text[i];
                        }
                        n = findindex;

                        Text_tmp = new Mystring(this.Length - findindex - s2.Length);
                        for (int i = findindex + s2.Length; i < this.Length; i++)
                        {
                            Text_tmp.Text[i - findindex - s2.Length] = Text[i];
                        }
                        Text_tmp2 += Text_tmp;
                        Text       = Text_tmp2.Text;
                    }
                }
            }
            for (int i = 0; i < Text.Length; i++)
            {
                chstr[i] = Text[i];
            }
            return(chstr);
        }
Пример #5
0
 public char[] ToArray(Mystring MyStr) // "Mystring" -> "char[]"
 {
     char[] res = new char[MyStr.Length];
     for (int i = 0; i < MyStr.Length; i++)
     {
         res[i] = MyStr.Text[i];
     }
     return(res);
 }
Пример #6
0
        public static Mystring ToArray(char[] arr) // "char[]" -> "Mystring"
        {
            int      p;
            Mystring res = new Mystring(arr.Length);

            p = arr.Length;
            for (int i = 0; i < p; i++)
            {
                res.Text[i] = arr[i];
            }
            return(res);
        }
Пример #7
0
        //перегрузка операторов

        public static Mystring operator +(Mystring s1, Mystring s2) // сложение строк
        {
            Mystring strsum = new Mystring(s1.Length + s2.Length);

            for (int i = 0; i < s1.Length; i++)
            {
                strsum.Text[i] = s1.Text[i];
            }
            for (int i = 0; i < s2.Length; i++)
            {
                strsum.Text[i + s1.Length] = s2.Text[i];
            }
            return(strsum);
        }
Пример #8
0
        public Mystring Replace(Mystring Str, char Old, char New, int num) //замена символа с num элемента- 2 ПЕРЕГРУЗКА
        {
            string TempText;

            TempText = Str; // Преобразование типов "Mystring" -> "string".
            int i = 0;

            foreach (char Simvol in TempText)
            {
                if ((i >= num) & (Simvol == Old))
                {
                    Str[i] = New;
                }
                i++;
            }
            return(Str);
        }
Пример #9
0
        public static int Compare(Mystring obj1, Mystring obj2) // Сравнение по алфавиту
        {
            int result = 0;
            int min    = Math.Min(obj1.Length, obj2.Length);

            for (int i = 0; i < min; i++)
            {
                if (obj1[i] > obj2[2])
                {
                    result = -1; // первая строка выше по алфавиту
                }
                else if (obj1[i] < obj2[2])
                {
                    result = 1; // первая ниже
                }
            }
            return(result); // если одинаковые
        }
Пример #10
0
        public Mystring ApplySubstring(Mystring obj, int Ind, int Count) // взятие подстроки с заданой длиной - 2 ПЕРЕГРУЗКА
        {
            string Text;

            Text = obj; // Преобразование типов "Mystring" -> "string".
            Mystring chstr = new Mystring();

            int i = 0;
            int j = 0;

            foreach (char Simvol in Text)
            {
                if ((i >= Ind) && (i <= Count + 1))
                {
                    chstr[j] += Text[i];
                    j++;
                }
                i++;
            }
            return(chstr);
        }
Пример #11
0
        //методы
        //1 метод
        public Mystring Replace(Mystring Str, char Old, char New, bool flag) //Замена символа - 1 ПЕРЕГРУЗКА
        {
            string TempText;

            TempText = Str; // Преобразование типов "Mystring" -> "string".
            int i = 0;

            foreach (char Simvol in TempText)
            {
                if (Simvol == Old)
                {
                    Str[i] = New;
                    if (flag)
                    {
                        break;
                    }
                }
                i++;
            }
            return(Str);
        }
Пример #12
0
        static void Main(string[] args)
        {
            Mystring myclass = new Mystring();
            Mystring str1, str2, str3;
            char     rem, rep;
            int      a, len;
            string   line, output;

            do
            {
                Console.Clear();
                Console.WriteLine("\n[1]: Подстрока\n[2]: Подстрока с заданной длиной\n[3]: Длина строки\n[4]: Сравнение (+/==/!=)\n[5]: Индекс символа в строке\n[6]: Замена символа в строке\n[7]: Поиск по строке\n[8]: Удаление\n[9]: Выйти ");
                Console.WriteLine("Введите команду:");
                string answer = Console.ReadLine();
                if (string.IsNullOrEmpty(answer))
                {
                    Console.WriteLine("Вы ничего не выбрали");
                    continue;
                }
                int comand = Convert.ToInt32(answer);

                switch (comand)
                {
                case 1:
                {
                    Console.WriteLine("Введите строку");
                    str1 = new Mystring(Console.ReadLine());
                    Console.WriteLine("После какого символа брать подстроку?");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Подстрока: " + myclass.ApplySubstring(str1, a));
                    Console.ReadKey();
                    break;
                }

                case 2:
                {
                    Console.WriteLine("Введите строку");
                    str1 = new Mystring(Console.ReadLine());
                    Console.WriteLine("После какого символа брать подстроку?");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Длина подстроки");
                    len = Convert.ToInt32(Console.ReadLine());
                    len--;
                    Console.WriteLine("Подстрока: " + myclass.ApplySubstring(str1, a, len));
                    Console.ReadKey();
                    break;
                }

                case 3:
                {
                    Console.WriteLine("Введите строку");
                    str1 = Console.ReadLine();
                    Console.WriteLine("Длина строки: " + myclass.Lenght(str1));
                    Console.ReadKey();
                    break;
                }

                case 4:
                {
                    Console.WriteLine("Введите строку");
                    str1 = new Mystring(Console.ReadLine());
                    Console.WriteLine("Введите еще одну строку");
                    str2 = new Mystring(Console.ReadLine());
                    Console.WriteLine("Сумма 2 строк: " + (str1 + str2));
                    if (str1 == str2)
                    {
                        Console.WriteLine("Строки одинаковые по длине");
                    }
                    else
                    {
                        Console.WriteLine("Строки не равны по длине");
                    }
                    Console.WriteLine(Mystring.Compare(str1, str2));
                    Console.ReadKey();
                    break;
                }

                case 5:
                {
                    Console.WriteLine("Введите строку");
                    str1 = Console.ReadLine();
                    line = Convert.ToString(str1);
                    Mystring mycl = new Mystring(line);
                    Console.WriteLine("Введите символ");
                    char ch = Convert.ToChar(Console.ReadLine());
                    Console.WriteLine("Новая строка: " + mycl.FindSymbol(ch));
                    Console.ReadKey();
                    break;
                }

                case 6:
                {
                    Console.WriteLine("Введите строку:");
                    str1 = new Mystring(Console.ReadLine());
                    Console.WriteLine("Введите заменяемый символ:");
                    rem = Convert.ToChar(Console.ReadLine());
                    Console.WriteLine("Введите заменяющий символ:");
                    rep = Convert.ToChar(Console.ReadLine());
                    // Console.WriteLine("Строка после замещения одного символа: " + myclass.Replace(str1, rem, rep, true));
                    // Console.WriteLine("Строка после замещения всех символов: " + myclass.Replace(str1, rem, rep, false));
                    Console.WriteLine("Введите номер символа с которого будет сделана замена:");
                    int num = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Строка после замещения всех символов после Num: " + myclass.Replace(str1, rem, rep, num));
                    Console.ReadKey();
                    break;
                }

                case 7:
                {
                    Console.WriteLine("Введите исходную строку:");
                    str1 = new Mystring(Console.ReadLine());
                    Console.WriteLine("Введите строку для поиска:");
                    str2 = new Mystring(Console.ReadLine());
                    Console.WriteLine("Задайте отступ:");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Индекс первого вхождения: " + str1.Index(str2));
                    Console.WriteLine("Индекс первого вхождения после отступа:" + str1.Index(str2, a));
                    Console.ReadKey();
                    break;
                }

                case 8:
                {
                    Console.WriteLine("Введите строку:");
                    str1 = new Mystring(Console.ReadLine());
                    str3 = new Mystring(str1);
                    Console.WriteLine("a)Индекс символа для начала удаления:");
                    a = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Количество удаляемых символов:");
                    len = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("б)Удаляемая подстрока:");
                    str2 = new Mystring(Console.ReadLine());
                    Console.WriteLine("a)Полученная строка: " + str1.Cut(a, len));
                    Console.WriteLine("б)Полученная строка: " + str3.Cut(str2));
                    Console.ReadKey();
                    break;
                }

                case 9: Environment.Exit(0); break;

                default: break;
                }
            } while (true);
        }