C#: Check if a given string contains two similar consecutive letters
C# Sharp Basic: Exercise-71 with Solution
Check Consecutive Similar Letters
Write a C# Sharp program to check if a given string contains two similar consecutive letters.
Sample Solution:
C# Sharp Code:
using System;
namespace exercises
{
class Program
{
// Main method where the program execution begins
static void Main(string[] args)
{
// Displays original strings and tests for consecutive similar letters
Console.WriteLine("Original string: PHP");
Console.WriteLine("Test for consecutive similar letters! " + test("PHP"));
Console.WriteLine("Original string: PHHP");
Console.WriteLine("Test for consecutive similar letters! " + test("PHHP"));
Console.WriteLine("Original string: PHPP");
Console.WriteLine("Test for consecutive similar letters! " + test("PHPP"));
Console.WriteLine("Original string: PPHP");
Console.WriteLine("Test for consecutive similar letters! " + test("PPHP"));
}
// Function to test if a string contains consecutive similar letters
public static bool test(string text)
{
// Iterates through the characters of the string except the last character
for (int i = 0; i < text.Length - 1; i++)
{
// Checks if the current character is the same as the next character
// If consecutive similar letters are found, returns true
if (text[i] == text[i + 1])
{
return true;
}
}
// Returns false if no consecutive similar letters are found in the string
return false;
}
}
}
Sample Output:
Original string: PHP Test for consecutive similar letters! False Original string: PHHP Test for consecutive similar letters! True Original string: PHPP Test for consecutive similar letters! True Original string: PPHP Test for consecutive similar letters! True
Flowchart:
Sample Solution-1:
C# Sharp Code:
using System;
using System.Text.RegularExpressions;
namespace exercises
{
class Program
{
// Main method where the program execution begins
static void Main(string[] args)
{
// Displays original strings and tests for consecutive similar letters
Console.WriteLine("Original string: PHP");
Console.WriteLine("Test for consecutive similar letters! " + test("PHP"));
Console.WriteLine("Original string: PHHP");
Console.WriteLine("Test for consecutive similar letters! " + test("PHHP"));
Console.WriteLine("Original string: PHPP");
Console.WriteLine("Test for consecutive similar letters! " + test("PHPP"));
Console.WriteLine("Original string: PPHP");
Console.WriteLine("Test for consecutive similar letters! " + test("PPHP"));
}
// Function to test if a string contains consecutive similar letters
public static bool test(string text)
{
// Uses Regex.IsMatch to check if the input string matches a pattern
// The pattern (.)\1 matches any character (captured by the group (.) represented by \1) that repeats consecutively
// If a match is found, it returns true, indicating consecutive similar letters
return Regex.IsMatch(text, @"(.)\1");
}
}
}
Sample Output:
Original string: PHP Test for consecutive similar letters! False Original string: PHHP Test for consecutive similar letters! True Original string: PHPP Test for consecutive similar letters! True Original string: PPHP Test for consecutive similar letters! True
Flowchart:
C# Sharp Code Editor:
Previous: Write a C# Sharp program to remove the first and last elements from a given string.
Next: Write a C# Sharp program to check whether the average value of the elements of a given array of numbers is a whole number or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/basic/csharp-basic-exercise-71.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics