Quick actions

cmd+k|ctrl+k

Navigation

Languages

checkPalindrome

Snippet info

Language

Csharp

Visibility

public

Author

dhaval-90

Created

2022-03-01T17:18:12.844764Z

Updated

2022-03-01T17:39:41.252378Z

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass {
    public static void Main() {
        Console.WriteLine("Hello World!");
        string str = "dad";
        checkPalindrome(str);
        if(checkPalindrome_2(str))
            Console.WriteLine("Palindrome from 2nd method");
        else
            Console.WriteLine("Not a Palindrome from 2nd method");
    }
    
    private static void checkPalindrome(string str)
    {
            bool flag = false; 
            for (int i = 0, j = str.Length - 1; i < str.Length / 2; i++, j--) 
            { 
                if (str[i] != str[j]) 
                { 
                     flag = false; 
                    break; 
                } 
                else 
                    flag = true; 
            } 
            if (flag) 
            { 
                Console.WriteLine("Palindrome"); 
            } 
            else 
                Console.WriteLine("Not Palindrome");
    }
    
    private static bool checkPalindrome_2(string myString)
    {
        int length = myString.Length;
        for (int i = 0; i < length / 2; i++)
        {
         if (myString[i] != myString[length - i - 1])
            return false;
        }
        return true;
    }
}
INFO