Remove Specified Characters

Write an efficient function in C# that deletes characters from a string. Use the prototype
String removeChars ( string str, string remove );
Where any characters existing in remove must be deleted from str. For example, given a str of “Battle of the Vowels: Hawaii vs. Grozny” and a remove of “aeiou”, the function should transform str to “Bttl f th Vwls: Hw vs. Grzny”.

O(n) implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RemoveCharacters
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Battle of the Vowels: Hawaii vs. Grozny";
            string remove = "aeiou";
            Console.Write("Enter a string: ");
            str = Console.ReadLine();
            Console.Write("Enter characters to be removed: ");
            remove = Console.ReadLine();
            Console.WriteLine("Actual String: " + str);
            Console.WriteLine("Characters to be removed: " + remove);
            Console.WriteLine("Modified String: " + removeChars(str, remove));
        }
        public static string removeChars(string str, string remove)
        {
            string tempStr = "";
            for (int i = 0; i < str.Length; i++)
            {
                if(!remove.Contains(str.Substring(i,1)))
                {
                    tempStr = tempStr + str.Substring(i,1);
                }
            }
            return tempStr;
        }
    }
}

O(n^2) implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RemoveCharacters
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Battle of the Vowels: Hawaii vs. Grozny";
            string remove = "aeiou";
            Console.Write("Enter a string: ");
            str = Console.ReadLine();
            Console.Write("Enter characters to be removed: ");
            remove = Console.ReadLine();
            Console.WriteLine("Actual String: " + str);
            Console.WriteLine("Characters to be removed: " + remove);
            Console.WriteLine("Modified String: " + removeChars(str, remove));
        }
        public static string removeChars(string str, string remove)
        {
            string tempStr;
            for (int i = 0; i < remove.Length; i++)
            {
                tempStr = "";
                for (int j = 0; j < str.Length; j++)
                {
                    if (remove.Substring(i, 1) != str.Substring(j, 1))
                    {
                        tempStr = tempStr + str.Substring(j, 1);
                    }
                }
                str = tempStr;
            }
            return str;
        }
    }
}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s