Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

palindrome number c#

    static bool IsPalindrome(int n)
    {
        return n.ToString() == Reverse(n.ToString());
    }

    static string Reverse(string str)
    {
        return new string(str.ToCharArray().Reverse().ToArray());
    }
Comment

Palindrome Number C#

Copy
public class Solution {
    public bool IsPalindrome(int x) {
        string first = x.ToString();             //turn to string (easy to reverse)
        char[] charArr = first.ToCharArray();   //the original target
        char[] reverseArr = first.ToCharArray();   

        Array.Reverse(reverseArr );                 
     
        return charArr.SequenceEqual(reverseArr); //compare two array
    }
}
Comment

Palindrome Number C# - better solution

Copy
public class Solution {
    public bool IsPalindrome(int x) {
        string k = x.ToString();        
        
        for(int i=0;i<k.Length/2;i++)   
        {
            if(k[i] != k[k.Length-1-i]) 
            {
                return false;           // if any char not the same, return false
            }
        }
        return true;                    
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to make a global string c# 
Csharp :: increase timeout in .net core web app 
Csharp :: c# check if string is all numbers 
Csharp :: cannot convert string to generic type c# 
Csharp :: how to read values from appsettings.json in c# 
Csharp :: make a list c# 
Csharp :: c# return switch 
Csharp :: randomm number from 2 different ranges 
Csharp :: unity RemoveComponent 
Csharp :: c# find element by condition 
Csharp :: how to make unity build to not be full screen 
Csharp :: c# read file 
Csharp :: make string uppercase c# 
Csharp :: how delete multiple row from relation in laravel 
Csharp :: defaultrequestheaders.authorization basic auth 
Csharp :: failed to read the request form. missing content-type boundary .net core 
Csharp :: C# get md5 of file 
Csharp :: change name of gameobject 
Csharp :: list index out of range c# 
Csharp :: check shell command success 
Csharp :: see if two string arrays are equal c# 
Csharp :: get width of image unity 
Csharp :: how to create a random vector2 in unity 
Csharp :: c# input 
Csharp :: unity health bar 
Csharp :: unity sound 
Csharp :: c# list 
Csharp :: c# how does comparing datetime work 
Csharp :: C# loop through array of objet 
Csharp :: hcf of numbers 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =