C#: Check if a given string contains 'w' character between 1 and 3 times
Check 'w' Appears 1-3 Times
Write a C# program to check if a given string contains the 'w' character between 1 and 3 times.
Pictorial Presentation:
Sample Solution:
C# Sharp Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Exercise41
{
public static void Main( )
{
// Prompt the user to input a string containing at least one 'w' character
Console.Write("Input a string (contains at least one 'w' char) : ");
// Read the input string and store it in the variable 'str'
string str = Console.ReadLine();
// Count the occurrences of the character 'w' in the input string and store the count in 'count'
var count = str.Count(s => s == 'w');
// Display a message indicating the test for the presence of 'w' character between 1 and 3 times
Console.WriteLine("Test the string contains 'w' character between 1 and 3 times: ");
// Check if the count of 'w' characters is between 1 and 3 (inclusive) and print the result
Console.WriteLine(count >= 1 && count <= 3);
}
}
Sample Output:
Input a string (contains at least one 'w' char) : w3resource Test the string contains 'w' character between 1 and 3 times: True
Flowchart:
C# Sharp Code Editor:
Previous: Write a C# program to check the nearest value of 20 of two given integers and return 0 if two numbers are same.
Next: 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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics