Write a C# Sharp program to check if a given string contains two similar consecutive letters

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to check if a given string contains two similar consecutive letters

Sample Output:

Original string: PHP
Test for consecutive similar letters! False
Original string: PHHP
Test for consecutive similar letters! True
Original string: PHPP
Test for consecutive similar letters! True
Original string: PPHP
Test for consecutive similar letters! True

الأجوبة

using System;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Original string: PHP");
            Console.WriteLine("Test for consecutive similar letters! " + test("PHP"));
            Console.WriteLine("Original string: PHHP");
            Console.WriteLine("Test for consecutive similar letters! " + test("PHHP"));
            Console.WriteLine("Original string: PHPP");
            Console.WriteLine("Test for consecutive similar letters! " + test("PHPP"));
            Console.WriteLine("Original string: PPHP");
            Console.WriteLine("Test for consecutive similar letters! " + test("PPHP"));
        }
        public static bool test(string text)
        {
            for (int i = 0; i < text.Length - 1; i++)
            {
                if (text[i] == text[i + 1]) 
                    { 
                      return true; 
                    }
            }
            return false;
        }
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...