Consecutive identical char in string

Write a program to check if the given string contains two consecutive identical char.

For example:

Input:
Good
India

Output:
True
False

Solution:



using System;

namespace Consecutive
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter text");
            var str = Console.ReadLine();


            Console.WriteLine(ConsecutiveChar(str));
            Console.ReadLine();

        }

        static bool ConsecutiveChar(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == str[i + 1])
                {
                    return true;
                }
            }

            return false;
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *