JulianJulianov

03.TextProcessingLab-Substring

Apr 14th, 2020
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. 03.Substring
  2. On the first line you will receive a string. On the second line you will receive a second string. Write a program that removes all of the occurrences of the first string in the second until there is no match. At the end print the remaining string.
  3. Examples
  4. Input           Output   Comment                                              Hints
  5. Ice             kgb      We remove ice once and we get "kgiciceeb"            Read the input.
  6. kicegiciceeb             We match "ice" one more time and we get "kgiceb"     Find the first index where the key appears.
  7.                          There is one more match. The finam result is "kgb"   Use the built-in method IndexOf()
  8.                                                                               Remove the match.
  9.                                                                               Use the built-in method Remove(index, length)
  10.                                                                               Repeat it until the text doesn't contain the key anymore.
  11. using System;
  12. namespace Substring
  13. {
  14.    class Program
  15.    {
  16.        static void Main(string[] args)
  17.        {
  18.            var firstString = Console.ReadLine().ToLower();
  19.            var secondString = Console.ReadLine().ToLower();
  20.            
  21.            while (secondString.IndexOf(firstString) != -1)
  22.            {
  23.                for (int i = 0; i <= secondString.Length - 1; i++)
  24.                {
  25.                    var matchedString = "";
  26.                    matchedString = secondString.Substring(i, firstString.Length);
  27.                    if (matchedString == firstString)
  28.                    {
  29.                        secondString = secondString.Remove(i, firstString.Length);
  30.                        //secondString = secondString.Replace(matchedString, string.Empty);  //Other way.
  31.                        break;
  32.                    }
  33.                }
  34.            }
  35.            Console.WriteLine($"{secondString}");
  36.        }
  37.    }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment