Q:

Write a C# Sharp program to check if the first appearance of "a" in a given string is immediately followed by another "a"

0

Write a C# Sharp program to check if the first appearance of "a" in a given string is immediately followed by another "a"

Sample Output:

True
False
True

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
namespace exercises
{
   class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(test("caabb"));
            Console.WriteLine(test("babaaba"));
            Console.WriteLine(test("aaaaa"));
            Console.ReadLine();
        }
        
        public static bool test(string str)
        {
            var counter = 0;
            for (var i = 0; i < str.Length-1; i++)
            {
                if (str[i].Equals('a')) counter++;
                if(str.Substring(i, 2).Equals("aa") && counter < 2) 
                return true;
            }
            return false;
        }
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now