Q:

Write a C# program to check if a given string starts with "w" and immediately followed by two "ww"

0

Write a C# program to check if a given string starts with "w" and immediately followed by two "ww"

Sample Output:

Input a string : www                                                   
False

All Answers

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Exercise43 {
static void Main(string[] args)
    {
            Console.Write("Input a string : ");
            string str = Console.ReadLine();
            Console.WriteLine(test(str));
    }
    public static bool test(string str)
       {
            var ctr = 0;
            for (var i = 0; i < str.Length-1; i++)
            {
                if (str[i].Equals('w')) ctr++;
                if(str.Substring(i, 2).Equals("ww") && ctr > 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