Function Palindrome, Iterative - C# Programming Exercise

In this exercise in C#, you need to create an iterative function that checks if a string is symmetric (a palindrome). A palindrome is a word, number, phrase, or other sequence of characters that reads the same forward and backward. For example, "RADAR" is a palindrome. Your iterative function should check if the given string is the same when reversed. This exercise in C# will help you improve your string manipulation skills and better understand the concept of palindromes, an interesting topic that appears in various programming applications. You will use loops and conditionals to compare the characters of the string from both ends.

After completing this exercise, you will be able to implement efficient solutions for detecting palindromes and performing string comparisons within your C# programs.

 Category

Functions

 Exercise

Function Palindrome, Iterative

 Objective

Write an C# iterative function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome.

 Write Your C# Exercise

// Import the System namespace to use basic classes like Console
using System;

class Program
{
    // Function to check if a string is a palindrome (symmetric)
    public static bool Palindrome(string str)
    {
        // Convert the string to uppercase to make the comparison case-insensitive
        str = str.ToUpper();

        // Loop through half of the string and compare each character from both ends
        for (int i = 0; i < str.Length / 2; i++)
        {
            // Compare the character at position i with the character at the corresponding position from the end
            if (str[i] != str[str.Length - i - 1])
            {
                // If any character does not match, it's not a palindrome
                return false;
            }
        }

        // If all characters match, it's a palindrome
        return true;
    }

    // Main function to test the Palindrome function
    public static void Main()
    {
        // Test case 1: "RADAR"
        string testString1 = "RADAR";
        Console.WriteLine($"Is \"{testString1}\" a palindrome? {Palindrome(testString1)}");

        // Test case 2: "HELLO"
        string testString2 = "HELLO";
        Console.WriteLine($"Is \"{testString2}\" a palindrome? {Palindrome(testString2)}");

        // Test case 3: "madam"
        string testString3 = "madam";
        Console.WriteLine($"Is \"{testString3}\" a palindrome? {Palindrome(testString3)}");
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function Palindrome, recursive

    In this exercise in C#, you will need to create a recursive function to check if a string is symmetric (a palindrome). A palindrome is a word, number, phrase, ...

  •  Function GetMinMax

    In this exercise in C#, you will need to write a function named "GetMinMax", which will ask the user to enter a minimum value (a number) and a maximum value (a...

  •  Function Multiply & MultiplyR

    In this exercise in C#, you will need to write two functions called "Multiply" and "MultiplyR" to calculate the product of two numbers using sums. The first ve...

  •  Functions: greeting + farewell

    In this C# exercise, you are asked to write a program where the main structure is the Main method. Inside this method, two functions should be called: SayHello...

  •  Function with parameters

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ SayHello("John"...

  •  Function returning a value

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ int x = 3;