C#: Check whether a given string starts with 'F' or ends with "B"
C# Sharp Basic Algorithm: Exercise-46 with Solution
Fizz, Buzz, FizzBuzz
Write a C# Sharp program to check whether a given string begins with "F" or ends with "B".
If the string starts with "F" return "Fizz" and return "Buzz" if it ends with "B" If the string starts with "F" and ends with "B" return "FizzBuzz".
In other cases return the original string.
Visual Presentation:
Sample Solution:-
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
// Class declaration
class Program
{
// Main method - entry point of the program
static void Main(string[] args)
{
// Calling the 'test' method with different strings and printing the results
Console.WriteLine(test("FizzBuzz")); // Output: FizzBuzz
Console.WriteLine(test("Fizz")); // Output: Fizz
Console.WriteLine(test("Buzz")); // Output: Buzz
Console.WriteLine(test("Founder")); // Output: Founder
Console.ReadLine(); // Keeping the console window open
}
// Method to determine the output based on specific conditions in the input string
public static string test(string str)
{
// Check if the string starts with "F" and ends with "B"
if (str.StartsWith("F") && str.EndsWith("B"))
{
return "FizzBuzz"; // If both conditions are true, return "FizzBuzz"
}
// Check if the string starts with "F"
else if (str.StartsWith("F"))
{
return "Fizz"; // If the string starts with "F", return "Fizz"
}
// Check if the string ends with "B"
else if (str.EndsWith("B"))
{
return "Buzz"; // If the string ends with "B", return "Buzz"
}
else
{
return str; // Return the original string if none of the conditions are met
}
}
}
}
Sample Output:
Fizz Fizz Buzz Fizz
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program check if a given number is within 2 of a multiple of 10.
Next: Write a C# Sharp program to check if it is possible to add two integers to get the third integer from three given integers
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-algo/csharp-basic-algorithm-exercises-46.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics