w3resource

C#: Check whether a given string ends with "on"


C# Sharp Basic Algorithm: Exercise-72 with Solution

Write a C# Sharp program to check whether a given string ends with "on".

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Check whether a given string ends with "on".

Sample Solution:-

C# Sharp Code:

using System; // Import the System namespace which contains fundamental types and base classes

namespace exercises // Define a namespace called 'exercises'
{
    class Program // Define a class named 'Program'
    {
        static void Main(string[] args) // The entry point of the program
        {
            // Output the result of the test function with different strings as arguments
            Console.WriteLine(test("Hello"));  // Check if "Hello" ends with "on"
            Console.WriteLine(test("Python")); // Check if "Python" ends with "on"
            Console.WriteLine(test("on"));     // Check if "on" ends with "on"
            Console.WriteLine(test("o"));      // Check if "o" ends with "on"
            Console.ReadLine(); // Wait for user input before closing the console window
        }

        // Function definition of test function that takes a string 's1' and returns a boolean value
        public static bool test(string s1)
        {
            // Check if the string 's1' ends with the substring "on"
            return s1.EndsWith("on");
        }
    }
}

Sample Output:

False
True
True
False

Flowchart:

C# Sharp: Flowchart: Check whether a given string ends with "on".

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to create a new string using the two middle characters of a given string of even length (at least 2).
Next: Write a C# Sharp program to create a new string using the first and last n characters from a given string of length at least n.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.