Distance Check

Check if the distance between two specified characters in a given string is less than or equal to a given distance.
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "abbbabbbaba";

            char a = ‘a’;

            char b = ‘a’;

            int distance = 1;

            Console.WriteLine(CheckDistance(str, a, b, distance));

        }

        public static bool CheckDistance(string str, char a, char b, int distance)

        {

            int flag = 0;

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

            {

                if (str[i] == a)

                {

                    for (int j = i + 1; j < str.Length; j++)

                    {

                        if (str[j] == b)

                        {

                            if ((j – i – 1) <= distance)

                            {

                                flag = 1;

                                break;

                            }

                        }

                    }

                }

            }

            if (flag == 1)

            {

                return true;

            }

            else

            {

                return false;

            }

        }

    }

}

True
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