We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Friday, June 19, 2015

Remove Duplicate Characters from a string using array

    class Program
    {
        static void Main(string[] args)
        {
            char[] ch = new char[30];
            string str;
            int count = 1, i, j;
            Console.WriteLine("Enter the string ");
            str = Console.ReadLine();
            int length = str.Length;
            for (i = 0; i < length; i++)
            {
                ch[i] = str[i];
            }
            for (i = 1; i < length; i++)
            {
                for (j = 0; j < i; j++)
                {
                    if (ch[i] == ch[j])
                        break;
                }
                if (j == i)
                {
                    ch[count] = ch[i];
                    count++;
                }
            }
            Console.WriteLine("After removing the duplicat character");
            {
                for (i = 0; i < count; i++)
                {
                    Console.Write(ch[i]);
                }
            }
            Console.WriteLine();
            Console.ReadLine();
        }
    }

OUTPUT:

No comments: