C#: Check if a given string starts with "w" and immediately followed by two "ww"
Check String Starts with 'www'
Write a C# program to check if a given string starts with "w" and is 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) {
// Prompt the user to input a string
Console.Write("Input a string : ");
// Read the input string and store it in the variable 'str'
string str = Console.ReadLine();
// Call the 'test' method with the input string and output the result
Console.WriteLine(test(str));
}
// Define a method named 'test' that takes a string parameter 'str' and returns a boolean value
public static bool test(string str) {
var ctr = 0; // Initialize a counter variable 'ctr' to count occurrences of 'w'
// Iterate through the characters of the string using a for loop
for (var i = 0; i < str.Length - 1; i++) {
// Check if the current character is 'w'; if so, increment the counter 'ctr'
if (str[i].Equals('w'))
ctr++;
// Check if the substring of length 2 starting at index 'i' contains "ww"
// and if the counter 'ctr' is greater than 2
if (str.Substring(i, 2).Equals("ww") && ctr > 2)
return true; // If the condition is met, return 'true'
}
return false; // Return 'false' if the condition is not met throughout the string
}
}
Sample Output:
Input a string : www False
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C# program to check if a string starts with "www" and ends with ".com".
- Write a C# program to validate a web address starting with "www" followed by letters and digits only.
- Write a C# program to check if the first three characters are all 'w'.
- Write a C# program to detect redundant "www" occurrences at the beginning of a string.
Go to:
PREV : First 4 Chars Lowercase, Rest Uppercase.
NEXT : Every Other Character in String.
C# Sharp Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.