Find the first repeated character in a word

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //Find the first repeated character in a word

            string word = "character";

            Hashtable ht = new Hashtable();

            for (int i = 0; i < word.Length; i++)

            {

                if (ht.Contains(word[i]))

                {

                    ht[word[i]] = int.Parse(ht[word[i]].ToString()) + 1;

                }

                else

                {

                    ht.Add(word[i], 1);

                }

            }

            int flag = 0;

            for (int i = 0; i < word.Length; i++)

            {

                if (int.Parse(ht[word[i]].ToString()) > 1)

                {

                    Console.WriteLine("First repeated character is: " + word[i]);

                    flag = 1;

                    break;

                }

            }

            if (flag == 0)

            {

                Console.WriteLine("No repeated characters found.");

            }

        }

    }

}
 
First repeated character is: c
Press any key to continue . . .
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