w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Check if a given string starts with "w" and immediately followed by two "ww"

C# Sharp Basic: Exercise-43 with Solution

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

Sample Solution:

C# Sharp Code:

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;
        }

}

Sample Output:

Input a string : www                                                   
False 

Flowchart:

Flowchart: C# Sharp Exercises - Check if a given string starts with

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to create a new string where the first 4 characters will be in lower case. If the string is less than 4 characters then make the whole string in upper case.
Next: Write a C# program to create a new string of every other character (odd position) from the first position of a given string.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming