Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 03.Substring
- 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.
- Examples
- Input Output Comment Hints
- Ice kgb We remove ice once and we get "kgiciceeb" Read the input.
- kicegiciceeb We match "ice" one more time and we get "kgiceb" Find the first index where the key appears.
- There is one more match. The finam result is "kgb" Use the built-in method IndexOf()
- Remove the match.
- Use the built-in method Remove(index, length)
- Repeat it until the text doesn't contain the key anymore.
- using System;
- namespace Substring
- {
- class Program
- {
- static void Main(string[] args)
- {
- var firstString = Console.ReadLine().ToLower();
- var secondString = Console.ReadLine().ToLower();
- while (secondString.IndexOf(firstString) != -1)
- {
- for (int i = 0; i <= secondString.Length - 1; i++)
- {
- var matchedString = "";
- matchedString = secondString.Substring(i, firstString.Length);
- if (matchedString == firstString)
- {
- secondString = secondString.Remove(i, firstString.Length);
- //secondString = secondString.Replace(matchedString, string.Empty); //Other way.
- break;
- }
- }
- }
- Console.WriteLine($"{secondString}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment