Friday 25 August 2017

Write a method which will remove any given character from a String

static void Main(string[] args)
        {
            string str = Console.ReadLine();

            char[] charArr = str.ToCharArray();
            
            Console.WriteLine("Enter the character to remove");
            char strChar= Convert.ToChar( Console.ReadLine());

            charArr = charArr.Where(x => x != strChar).ToArray();


            str = new string(charArr);
            int counter = 0;
            for (int i = 0;  i<str.Length;i++ )
            {
                if (str[i] == strChar)
                {
                   str=  str.Remove(i,1);
                   i--;
                    counter++;
                    Console.WriteLine(i +"th index match");
                }


            }

            Console.WriteLine(counter + " times match");
            Console.WriteLine("Final String after char removal= "+ str);
            

 


                Console.ReadLine();
        }

No comments:

Post a Comment

Recent Post

Parallel Task in .Net 4.0