Check if a string is a palindrome
I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic.
How can I do this?
Sample:
public bool getStatus(string myString)
{
}
Example: myString = "ankYkna"
, so if we break it into two substring it would be:
left-part = "ank"
,
right-part = "ank"
(after reversal).
c# string palindrome
|
show 2 more comments
I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic.
How can I do this?
Sample:
public bool getStatus(string myString)
{
}
Example: myString = "ankYkna"
, so if we break it into two substring it would be:
left-part = "ank"
,
right-part = "ank"
(after reversal).
c# string palindrome
What's your actual problem here? Just usemyString.SubString()
.
– ken2k
Mar 20 '12 at 16:23
@ken2k how to break a string into two equal substring.
– ankur
Mar 20 '12 at 16:24
1
So you just want to check if the string is an anagram?
– Thomas Levesque
Mar 20 '12 at 16:25
@ThomasLevesque yup.
– ankur
Mar 20 '12 at 16:26
3
Or just a palindrome?
– cadrell0
Mar 20 '12 at 16:27
|
show 2 more comments
I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic.
How can I do this?
Sample:
public bool getStatus(string myString)
{
}
Example: myString = "ankYkna"
, so if we break it into two substring it would be:
left-part = "ank"
,
right-part = "ank"
(after reversal).
c# string palindrome
I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic.
How can I do this?
Sample:
public bool getStatus(string myString)
{
}
Example: myString = "ankYkna"
, so if we break it into two substring it would be:
left-part = "ank"
,
right-part = "ank"
(after reversal).
c# string palindrome
c# string palindrome
edited Jan 30 at 19:04
yaken
5917
5917
asked Mar 20 '12 at 16:21
ankurankur
2,005124575
2,005124575
What's your actual problem here? Just usemyString.SubString()
.
– ken2k
Mar 20 '12 at 16:23
@ken2k how to break a string into two equal substring.
– ankur
Mar 20 '12 at 16:24
1
So you just want to check if the string is an anagram?
– Thomas Levesque
Mar 20 '12 at 16:25
@ThomasLevesque yup.
– ankur
Mar 20 '12 at 16:26
3
Or just a palindrome?
– cadrell0
Mar 20 '12 at 16:27
|
show 2 more comments
What's your actual problem here? Just usemyString.SubString()
.
– ken2k
Mar 20 '12 at 16:23
@ken2k how to break a string into two equal substring.
– ankur
Mar 20 '12 at 16:24
1
So you just want to check if the string is an anagram?
– Thomas Levesque
Mar 20 '12 at 16:25
@ThomasLevesque yup.
– ankur
Mar 20 '12 at 16:26
3
Or just a palindrome?
– cadrell0
Mar 20 '12 at 16:27
What's your actual problem here? Just use
myString.SubString()
.– ken2k
Mar 20 '12 at 16:23
What's your actual problem here? Just use
myString.SubString()
.– ken2k
Mar 20 '12 at 16:23
@ken2k how to break a string into two equal substring.
– ankur
Mar 20 '12 at 16:24
@ken2k how to break a string into two equal substring.
– ankur
Mar 20 '12 at 16:24
1
1
So you just want to check if the string is an anagram?
– Thomas Levesque
Mar 20 '12 at 16:25
So you just want to check if the string is an anagram?
– Thomas Levesque
Mar 20 '12 at 16:25
@ThomasLevesque yup.
– ankur
Mar 20 '12 at 16:26
@ThomasLevesque yup.
– ankur
Mar 20 '12 at 16:26
3
3
Or just a palindrome?
– cadrell0
Mar 20 '12 at 16:27
Or just a palindrome?
– cadrell0
Mar 20 '12 at 16:27
|
show 2 more comments
29 Answers
29
active
oldest
votes
public static bool getStatus(string myString)
{
string first = myString.Substring(0, myString.Length / 2);
char arr = myString.ToCharArray();
Array.Reverse(arr);
string temp = new string(arr);
string second = temp.Substring(0, temp.Length / 2);
return first.Equals(second);
}
This actually will not work for palindromes with punctuation and asymetrical whitespace. Example: "No 'X' in Nixon." Is a palindrome.
– Espen
Nov 18 '16 at 10:15
add a comment |
Just for fun:
return myString.SequenceEqual(myString.Reverse());
1
Missingusing System.Linq;
– Junior M
Apr 11 '17 at 20:43
add a comment |
int length = myString.Length;
for (int i = 0; i < length / 2; i++)
{
if (myString[i] != myString[length - i - 1])
return false;
}
return true;
Short and concise. My compliments.
– M. Mimpen
May 22 '15 at 14:23
Marginally quicker would be to save thelength / 2
within its own variable.
– krillgar
Feb 12 '16 at 18:24
add a comment |
Using LINQ and off course far from the best solution
var original = "ankYkna";
var reversed = new string(original.Reverse().ToArray());
var palindrom = original == reversed;
A string is inherently a character array, so the.ToCharArray()
call is redundant.
– krillgar
Feb 12 '16 at 18:23
@krillgar you're right, string implements IEnumerable<char> so the ToCharArray is not needed. I can't tell if it was like this back in 2012.
– Adrian Iftode
Feb 12 '16 at 19:45
2
I am pretty sure it was always the case, but people were so explicit when LINQ first came out. I just stumbled across this question and wanted to point that out to anyone else who came across your solid answer.
– krillgar
Feb 12 '16 at 19:47
1
for case sensitivity do add String.Equals(word,original,StringComparison.OrdinalIgnoreCase)
– afr0
Jun 14 '17 at 13:46
add a comment |
public static bool IsPalindrome(string value)
{
int i = 0;
int j = value.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = value[i];
char b = value[j];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
i++;
j--;
}
}
add a comment |
A single line of code using Linq
public static bool IsPalindrome(string str)
{
return str.SequenceEqual(str.Reverse());
}
add a comment |
//This c# method will check for even and odd lengh palindrome string
public static bool IsPalenDrome(string palendromeString)
{
bool isPalenDrome = false;
try
{
int halfLength = palendromeString.Length / 2;
string leftHalfString = palendromeString.Substring(0,halfLength);
char reversedArray = palendromeString.ToCharArray();
Array.Reverse(reversedArray);
string reversedString = new string(reversedArray);
string rightHalfStringReversed = reversedString.Substring(0, halfLength);
isPalenDrome = leftHalfString == rightHalfStringReversed ? true : false;
}
catch (Exception ex)
{
throw ex;
}
return isPalenDrome;
}
add a comment |
This way is both concise in appearance & processes very quickly.
Func<string, bool> IsPalindrome = s => s.Reverse().Equals(s);
3
It's also dead wrong, since it will always returnfalse
.s.Reverse()
is anIEnumerable<char>
, which never compares reference-equal to the originalString
(assuming the standard definition ofEnumerable.Reverse
andObject.Equals
).
– Jeroen Mostert
Apr 13 '17 at 12:01
add a comment |
String extension method, easy to use:
public static bool IsPalindrome(this string str)
{
str = new Regex("[^a-zA-Z]").Replace(str, "").ToLower();
return !str.Where((t, i) => t != str[str.Length - i - 1]).Any();
}
add a comment |
private void CheckIfPalindrome(string str)
{
//place string in array of chars
char array = str.ToCharArray();
int length = array.Length -1 ;
Boolean palindrome =true;
for (int i = 0; i <= length; i++)//go through the array
{
if (array[i] != array[length])//compare if the char in the same positions are the same eg "tattarrattat" will compare array[0]=t with array[11] =t if are not the same stop the for loop
{
MessageBox.Show("not");
palindrome = false;
break;
}
else //if they are the same make length smaller by one and do the same
{
length--;
}
}
if (palindrome) MessageBox.Show("Palindrome");
}
1
Please provide an explanation with your answer.
– Winter
Jul 11 '17 at 13:39
Code only answers arent encouraged as they dont provide much information for future readers
– WhatsThePoint
Jul 11 '17 at 13:45
I have an explanation in the comment area what else should i explain?
– pilot13
Mar 8 '18 at 11:35
add a comment |
If you just need to detect a palindrome, you can do it with a regex, as explained here. Probably not the most efficient approach, though...
add a comment |
That is non-trivial, there is no built in method to do that for you, you'll have to write your own. You will need to consider what rules you would like to check, like you implicitly stated you accepted reversing of one string. Also, you missed out the middle character, is this only if odd length?
So you will have something like:
if(myString.length % 2 = 0)
{
//even
string a = myString.substring(0, myString.length / 2);
string b = myString.substring(myString.length / 2 + 1, myString.lenght/2);
if(a == b)
return true;
//Rule 1: reverse
if(a == b.reverse()) //can't remember if this is a method, if not you'll have to write that too
return true;
etc, also doing whatever you want for odd strings
add a comment |
This C# method will check for even and odd length palindrome string (Recursive Approach):
public static bool IsPalindromeResursive(int rightIndex, int leftIndex, char inputString)
{
if (rightIndex == leftIndex || rightIndex < leftIndex)
return true;
if (inputString[rightIndex] == inputString[leftIndex])
return IsPalindromeResursive(--rightIndex, ++leftIndex, inputString);
else
return false;
}
add a comment |
public Boolean IsPalindrome(string value)
{
var one = value.ToList<char>();
var two = one.Reverse<char>().ToList();
return one.Equals(two);
}
add a comment |
protected bool CheckIfPalindrome(string text)
{
if (text != null)
{
string strToUpper = Text.ToUpper();
char toReverse = strToUpper.ToCharArray();
Array.Reverse(toReverse );
String strReverse = new String(toReverse);
if (strToUpper == toReverse)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Use this the sipmlest way.
cannot compare string to char
– wruckie
Jan 13 '15 at 0:37
add a comment |
class Program
{
static void Main(string args)
{
string s, revs = "";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
Console.WriteLine(i);
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome");
}
else
{
Console.WriteLine("String is not Palindrome");
}
Console.ReadKey();
}
}
add a comment |
public bool IsPalindroom(string input)
{
input = input.ToLower();
var loops = input.Length / 2;
var higherBoundIdx = input.Length - 1;
for (var lowerBoundIdx = 0; lowerBoundIdx < loops; lowerBoundIdx++, higherBoundIdx--)
{
if (input[lowerBoundIdx] != input[higherBoundIdx])
return false;
}
return true;
}
add a comment |
public static bool IsPalindrome(string word)
{
//first reverse the string
string reversedString = new string(word.Reverse().ToArray());
return string.Compare(word, reversedString) == 0 ? true : false;
}
add a comment |
Here is an absolutely simple way to do this,
- Receive the word as input into a method.
- Assign a temp variable to the original value.
- Loop through the initial word, and add the last character to the reversal that you are constructing until the inital word has no more characters.
- Now use the spare you created to hold the original value to compare to the constructed copy.
This is a nice way as u don't have to cast ints and doubles. U can just pass them to the method in their string representation by using the ToString() method.
public static bool IsPalindrome(string word)
{
string spare = word;
string reversal = null;
while (word.Length > 0)
{
reversal = string.Concat(reversal, word.LastOrDefault());
word = word.Remove(word.Length - 1);
}
return spare.Equals(reversal);
}
So from your main method,
For even and odd length strings u just pass the whole string into the method.
add a comment |
Out of all the solutions, below can also be tried:
public static bool IsPalindrome(string s)
{
return s == new string(s.Reverse().ToArray());
}
add a comment |
Since a palindrome also includes numbers, words, sentences, and any combinations of these, and should ignore punctuation and case, (See Wikipedia Article)
I propose this solution:
public class Palindrome
{
static IList<int> Allowed = new List<int> {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9',
'0'
};
private static int GetJustAllowed(string text)
{
List<int> characters = new List<int>();
foreach (var c in text)
characters.Add(c | 0x20);
return characters.Where(c => Allowed.Contains(c)).ToArray();
}
public static bool IsPalindrome(string text)
{
if(text == null || text.Length == 1)
return true;
int chars = GetJustAllowed(text);
var length = chars.Length;
while (length > 0)
if (chars[chars.Length - length] != chars[--length])
return false;
return true;
}
public static bool IsPalindrome(int number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(double number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(decimal number)
{
return IsPalindrome(number.ToString());
}
}
The allowed list should certainly be extended to include more languages and concepts than just English, but I hope to illustrate that just checking if string.reverse() == string is not a complete solution to the palindrome problem.
– Espen
Oct 24 '16 at 20:05
add a comment |
static void Main(string args)
{
string str, rev="";
Console.Write("Enter string");
str = Console.ReadLine();
for (int i = str.Length - 1; i >= 0; i--)
{
rev = rev + str[i];
}
if (rev == str)
Console.Write("Entered string is pallindrome");
else
Console.Write("Entered string is not pallindrome");
Console.ReadKey();
}
add a comment |
string test = "Malayalam";
char palindrome = test.ToCharArray();
char reversestring = new char[palindrome.Count()];
for (int i = palindrome.Count() - 1; i >= 0; i--)
{
reversestring[palindrome.Count() - 1 - i] = palindrome[i];
}
string materializedString = new string(reversestring);
if (materializedString.ToLower() == test.ToLower())
{
Console.Write("Palindrome!");
}
else
{
Console.Write("Not a Palindrome!");
}
Console.Read();
add a comment |
public static bool palindrome(string t)
{
int i = t.Length;
for (int j = 0; j < i / 2; j++)
{
if (t[j] == t[i - j-1])
{
continue;
}
else
{
return false;
break;
}
}
return true;
}
1
Please, next time, along with the code enter a short explanation.
– Kerberos
Nov 4 '17 at 9:36
add a comment |
use this way from dotnetperls
using System;
class Program
{
/// <summary>
/// Determines whether the string is a palindrome.
/// </summary>
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
// Scan forward for a while invalid.
while (!char.IsLetterOrDigit(a))
{
min++;
if (min > max)
{
return true;
}
a = value[min];
}
// Scan backward for b while invalid.
while (!char.IsLetterOrDigit(b))
{
max--;
if (min > max)
{
return true;
}
b = value[max];
}
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
static void Main()
{
string array =
{
"A man, a plan, a canal: Panama.",
"A Toyota. Race fast, safe car. A Toyota.",
"Cigar? Toss it in a can. It is so tragic.",
"Dammit, I'm mad!",
"Delia saw I was ailed.",
"Desserts, I stressed!",
"Draw, O coward!",
"Lepers repel.",
"Live not on evil.",
"Lonely Tylenol.",
"Murder for a jar of red rum.",
"Never odd or even.",
"No lemon, no melon.",
"Senile felines.",
"So many dynamos!",
"Step on no pets.",
"Was it a car or a cat I saw?",
"Dot Net Perls is not a palindrome.",
"Why are you reading this?",
"This article is not useful.",
"...",
"...Test"
};
foreach (string value in array)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
}
}
Please add description to your code explaining what it does
– Rahul Gupta
Dec 22 '17 at 11:28
see the comment at the top of IsPalindrome function :)
– FAREH
Dec 22 '17 at 13:05
add a comment |
This is a short and efficient way of checking palindrome.
bool checkPalindrome(string inputString) {
int length = inputString.Length;
for(int i = 0; i < length/2; i++){
if(inputString[i] != inputString[length-1-i]){
return false;
}
}
return true;
}
add a comment |
public bool Solution(string content)
{
int length = content.Length;
int half = length/2;
int isOddLength = length%2;
// Counter for checking the string from the middle
int j = (isOddLength==0) ? half:half+1;
for(int i=half-1;i>=0;i--)
{
if(content[i] != content[j])
{
return false;
}
j++;
}
return true;
}
Bonus points if you add some details on how this works for future visitors. A couple of test cases, too, maybe?
– Alexander van Oostenrijk
Aug 7 '18 at 20:08
add a comment |
public bool MojTestPalindrome (string word)
{
bool yes = false;
chartest1 = word.ToArray();
char test2 = test1.Reverse().ToArray();
for (int i=0; i< test2.Length; i++)
{
if (test1[i] != test2[test2.Length - 1 - i])
{
yes = false;
break;
}
else {
yes = true;
}
}
if (yes == true)
{
return true;
}
else
return false;
}
add a comment |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class palindrome
{
static void Main(string args)
{
Console.Write("Enter a number:");
string panstring = Console.ReadLine();
Palindrome(panstring);
Console.ReadKey();
}
static int index = 0;
public static void Palindrome(string strexcluding)
{
try
{
string reversecounter = string.Empty;
for (int i = strexcluding.Length - 1; i >= 0; i--)
{
if (strexcluding[i].ToString() != null)
reversecounter += strexcluding[i].ToString();
}
if (reversecounter == strexcluding)
{
Console.WriteLine("Palindrome Number: " + strexcluding);
}
else
{
Sum(strexcluding);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void Sum(string stringnumber)
{
try
{
index++;
string number1 = stringnumber;
string number2 = stringnumber;
string array = new string[number1.Length];
string obtained = string.Empty;
string sreverse = null;
Console.WriteLine(index + ".step : " + number1 + "+" + number2);
for (int i = 0; i < number1.Length; i++)
{
int temp1 = Convert.ToInt32(number1[number1.Length - i - 1].ToString());
int temp2 = Convert.ToInt32(number2[number2.Length - i - 1].ToString());
if (temp1 + temp2 >= 10)
{
if (number1.Length - 1 == number1.Length - 1 - i)
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
else if (number1.Length - 1 == i)
{
if (obtained == "one")
{
array[i] = (temp1 + temp2 + 1).ToString();
}
else
{
array[i] = (temp1 + temp2).ToString();
}
}
else
{
if (obtained == "one")
{
array[i] = ((temp1 + temp2 + 1) - 10).ToString();
}
else
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
}
}
else
{
if (obtained == "one")
array[i] = (temp1 + temp2 + 1).ToString();
else
array[i] = (temp1 + temp2).ToString();
obtained = "Zero";
}
}
for (int i = array.Length - 1; i >= 0; i--)
{
if (array[i] != null)
sreverse += array[i].ToString();
}
Palindrome(sreverse);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9790749%2fcheck-if-a-string-is-a-palindrome%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
29 Answers
29
active
oldest
votes
29 Answers
29
active
oldest
votes
active
oldest
votes
active
oldest
votes
public static bool getStatus(string myString)
{
string first = myString.Substring(0, myString.Length / 2);
char arr = myString.ToCharArray();
Array.Reverse(arr);
string temp = new string(arr);
string second = temp.Substring(0, temp.Length / 2);
return first.Equals(second);
}
This actually will not work for palindromes with punctuation and asymetrical whitespace. Example: "No 'X' in Nixon." Is a palindrome.
– Espen
Nov 18 '16 at 10:15
add a comment |
public static bool getStatus(string myString)
{
string first = myString.Substring(0, myString.Length / 2);
char arr = myString.ToCharArray();
Array.Reverse(arr);
string temp = new string(arr);
string second = temp.Substring(0, temp.Length / 2);
return first.Equals(second);
}
This actually will not work for palindromes with punctuation and asymetrical whitespace. Example: "No 'X' in Nixon." Is a palindrome.
– Espen
Nov 18 '16 at 10:15
add a comment |
public static bool getStatus(string myString)
{
string first = myString.Substring(0, myString.Length / 2);
char arr = myString.ToCharArray();
Array.Reverse(arr);
string temp = new string(arr);
string second = temp.Substring(0, temp.Length / 2);
return first.Equals(second);
}
public static bool getStatus(string myString)
{
string first = myString.Substring(0, myString.Length / 2);
char arr = myString.ToCharArray();
Array.Reverse(arr);
string temp = new string(arr);
string second = temp.Substring(0, temp.Length / 2);
return first.Equals(second);
}
edited Jul 17 '18 at 22:33
AustinWBryan
2,35121332
2,35121332
answered Mar 20 '12 at 16:31
iondenionden
10.4k13333
10.4k13333
This actually will not work for palindromes with punctuation and asymetrical whitespace. Example: "No 'X' in Nixon." Is a palindrome.
– Espen
Nov 18 '16 at 10:15
add a comment |
This actually will not work for palindromes with punctuation and asymetrical whitespace. Example: "No 'X' in Nixon." Is a palindrome.
– Espen
Nov 18 '16 at 10:15
This actually will not work for palindromes with punctuation and asymetrical whitespace. Example: "No 'X' in Nixon." Is a palindrome.
– Espen
Nov 18 '16 at 10:15
This actually will not work for palindromes with punctuation and asymetrical whitespace. Example: "No 'X' in Nixon." Is a palindrome.
– Espen
Nov 18 '16 at 10:15
add a comment |
Just for fun:
return myString.SequenceEqual(myString.Reverse());
1
Missingusing System.Linq;
– Junior M
Apr 11 '17 at 20:43
add a comment |
Just for fun:
return myString.SequenceEqual(myString.Reverse());
1
Missingusing System.Linq;
– Junior M
Apr 11 '17 at 20:43
add a comment |
Just for fun:
return myString.SequenceEqual(myString.Reverse());
Just for fun:
return myString.SequenceEqual(myString.Reverse());
answered Mar 20 '12 at 18:21
Balazs TihanyiBalazs Tihanyi
5,30441622
5,30441622
1
Missingusing System.Linq;
– Junior M
Apr 11 '17 at 20:43
add a comment |
1
Missingusing System.Linq;
– Junior M
Apr 11 '17 at 20:43
1
1
Missing
using System.Linq;
– Junior M
Apr 11 '17 at 20:43
Missing
using System.Linq;
– Junior M
Apr 11 '17 at 20:43
add a comment |
int length = myString.Length;
for (int i = 0; i < length / 2; i++)
{
if (myString[i] != myString[length - i - 1])
return false;
}
return true;
Short and concise. My compliments.
– M. Mimpen
May 22 '15 at 14:23
Marginally quicker would be to save thelength / 2
within its own variable.
– krillgar
Feb 12 '16 at 18:24
add a comment |
int length = myString.Length;
for (int i = 0; i < length / 2; i++)
{
if (myString[i] != myString[length - i - 1])
return false;
}
return true;
Short and concise. My compliments.
– M. Mimpen
May 22 '15 at 14:23
Marginally quicker would be to save thelength / 2
within its own variable.
– krillgar
Feb 12 '16 at 18:24
add a comment |
int length = myString.Length;
for (int i = 0; i < length / 2; i++)
{
if (myString[i] != myString[length - i - 1])
return false;
}
return true;
int length = myString.Length;
for (int i = 0; i < length / 2; i++)
{
if (myString[i] != myString[length - i - 1])
return false;
}
return true;
answered Mar 20 '12 at 16:35
Balazs TihanyiBalazs Tihanyi
5,30441622
5,30441622
Short and concise. My compliments.
– M. Mimpen
May 22 '15 at 14:23
Marginally quicker would be to save thelength / 2
within its own variable.
– krillgar
Feb 12 '16 at 18:24
add a comment |
Short and concise. My compliments.
– M. Mimpen
May 22 '15 at 14:23
Marginally quicker would be to save thelength / 2
within its own variable.
– krillgar
Feb 12 '16 at 18:24
Short and concise. My compliments.
– M. Mimpen
May 22 '15 at 14:23
Short and concise. My compliments.
– M. Mimpen
May 22 '15 at 14:23
Marginally quicker would be to save the
length / 2
within its own variable.– krillgar
Feb 12 '16 at 18:24
Marginally quicker would be to save the
length / 2
within its own variable.– krillgar
Feb 12 '16 at 18:24
add a comment |
Using LINQ and off course far from the best solution
var original = "ankYkna";
var reversed = new string(original.Reverse().ToArray());
var palindrom = original == reversed;
A string is inherently a character array, so the.ToCharArray()
call is redundant.
– krillgar
Feb 12 '16 at 18:23
@krillgar you're right, string implements IEnumerable<char> so the ToCharArray is not needed. I can't tell if it was like this back in 2012.
– Adrian Iftode
Feb 12 '16 at 19:45
2
I am pretty sure it was always the case, but people were so explicit when LINQ first came out. I just stumbled across this question and wanted to point that out to anyone else who came across your solid answer.
– krillgar
Feb 12 '16 at 19:47
1
for case sensitivity do add String.Equals(word,original,StringComparison.OrdinalIgnoreCase)
– afr0
Jun 14 '17 at 13:46
add a comment |
Using LINQ and off course far from the best solution
var original = "ankYkna";
var reversed = new string(original.Reverse().ToArray());
var palindrom = original == reversed;
A string is inherently a character array, so the.ToCharArray()
call is redundant.
– krillgar
Feb 12 '16 at 18:23
@krillgar you're right, string implements IEnumerable<char> so the ToCharArray is not needed. I can't tell if it was like this back in 2012.
– Adrian Iftode
Feb 12 '16 at 19:45
2
I am pretty sure it was always the case, but people were so explicit when LINQ first came out. I just stumbled across this question and wanted to point that out to anyone else who came across your solid answer.
– krillgar
Feb 12 '16 at 19:47
1
for case sensitivity do add String.Equals(word,original,StringComparison.OrdinalIgnoreCase)
– afr0
Jun 14 '17 at 13:46
add a comment |
Using LINQ and off course far from the best solution
var original = "ankYkna";
var reversed = new string(original.Reverse().ToArray());
var palindrom = original == reversed;
Using LINQ and off course far from the best solution
var original = "ankYkna";
var reversed = new string(original.Reverse().ToArray());
var palindrom = original == reversed;
edited Feb 12 '16 at 19:46
answered Mar 20 '12 at 16:37
Adrian IftodeAdrian Iftode
13.5k23565
13.5k23565
A string is inherently a character array, so the.ToCharArray()
call is redundant.
– krillgar
Feb 12 '16 at 18:23
@krillgar you're right, string implements IEnumerable<char> so the ToCharArray is not needed. I can't tell if it was like this back in 2012.
– Adrian Iftode
Feb 12 '16 at 19:45
2
I am pretty sure it was always the case, but people were so explicit when LINQ first came out. I just stumbled across this question and wanted to point that out to anyone else who came across your solid answer.
– krillgar
Feb 12 '16 at 19:47
1
for case sensitivity do add String.Equals(word,original,StringComparison.OrdinalIgnoreCase)
– afr0
Jun 14 '17 at 13:46
add a comment |
A string is inherently a character array, so the.ToCharArray()
call is redundant.
– krillgar
Feb 12 '16 at 18:23
@krillgar you're right, string implements IEnumerable<char> so the ToCharArray is not needed. I can't tell if it was like this back in 2012.
– Adrian Iftode
Feb 12 '16 at 19:45
2
I am pretty sure it was always the case, but people were so explicit when LINQ first came out. I just stumbled across this question and wanted to point that out to anyone else who came across your solid answer.
– krillgar
Feb 12 '16 at 19:47
1
for case sensitivity do add String.Equals(word,original,StringComparison.OrdinalIgnoreCase)
– afr0
Jun 14 '17 at 13:46
A string is inherently a character array, so the
.ToCharArray()
call is redundant.– krillgar
Feb 12 '16 at 18:23
A string is inherently a character array, so the
.ToCharArray()
call is redundant.– krillgar
Feb 12 '16 at 18:23
@krillgar you're right, string implements IEnumerable<char> so the ToCharArray is not needed. I can't tell if it was like this back in 2012.
– Adrian Iftode
Feb 12 '16 at 19:45
@krillgar you're right, string implements IEnumerable<char> so the ToCharArray is not needed. I can't tell if it was like this back in 2012.
– Adrian Iftode
Feb 12 '16 at 19:45
2
2
I am pretty sure it was always the case, but people were so explicit when LINQ first came out. I just stumbled across this question and wanted to point that out to anyone else who came across your solid answer.
– krillgar
Feb 12 '16 at 19:47
I am pretty sure it was always the case, but people were so explicit when LINQ first came out. I just stumbled across this question and wanted to point that out to anyone else who came across your solid answer.
– krillgar
Feb 12 '16 at 19:47
1
1
for case sensitivity do add String.Equals(word,original,StringComparison.OrdinalIgnoreCase)
– afr0
Jun 14 '17 at 13:46
for case sensitivity do add String.Equals(word,original,StringComparison.OrdinalIgnoreCase)
– afr0
Jun 14 '17 at 13:46
add a comment |
public static bool IsPalindrome(string value)
{
int i = 0;
int j = value.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = value[i];
char b = value[j];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
i++;
j--;
}
}
add a comment |
public static bool IsPalindrome(string value)
{
int i = 0;
int j = value.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = value[i];
char b = value[j];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
i++;
j--;
}
}
add a comment |
public static bool IsPalindrome(string value)
{
int i = 0;
int j = value.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = value[i];
char b = value[j];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
i++;
j--;
}
}
public static bool IsPalindrome(string value)
{
int i = 0;
int j = value.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = value[i];
char b = value[j];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
i++;
j--;
}
}
edited Oct 28 '16 at 11:33
Adrian Iftode
13.5k23565
13.5k23565
answered Jul 18 '14 at 2:58
GANIGANI
1,21922253
1,21922253
add a comment |
add a comment |
A single line of code using Linq
public static bool IsPalindrome(string str)
{
return str.SequenceEqual(str.Reverse());
}
add a comment |
A single line of code using Linq
public static bool IsPalindrome(string str)
{
return str.SequenceEqual(str.Reverse());
}
add a comment |
A single line of code using Linq
public static bool IsPalindrome(string str)
{
return str.SequenceEqual(str.Reverse());
}
A single line of code using Linq
public static bool IsPalindrome(string str)
{
return str.SequenceEqual(str.Reverse());
}
edited May 17 '18 at 9:50
fubo
29.8k967104
29.8k967104
answered Dec 16 '12 at 3:01
Ernesto CejasErnesto Cejas
764
764
add a comment |
add a comment |
//This c# method will check for even and odd lengh palindrome string
public static bool IsPalenDrome(string palendromeString)
{
bool isPalenDrome = false;
try
{
int halfLength = palendromeString.Length / 2;
string leftHalfString = palendromeString.Substring(0,halfLength);
char reversedArray = palendromeString.ToCharArray();
Array.Reverse(reversedArray);
string reversedString = new string(reversedArray);
string rightHalfStringReversed = reversedString.Substring(0, halfLength);
isPalenDrome = leftHalfString == rightHalfStringReversed ? true : false;
}
catch (Exception ex)
{
throw ex;
}
return isPalenDrome;
}
add a comment |
//This c# method will check for even and odd lengh palindrome string
public static bool IsPalenDrome(string palendromeString)
{
bool isPalenDrome = false;
try
{
int halfLength = palendromeString.Length / 2;
string leftHalfString = palendromeString.Substring(0,halfLength);
char reversedArray = palendromeString.ToCharArray();
Array.Reverse(reversedArray);
string reversedString = new string(reversedArray);
string rightHalfStringReversed = reversedString.Substring(0, halfLength);
isPalenDrome = leftHalfString == rightHalfStringReversed ? true : false;
}
catch (Exception ex)
{
throw ex;
}
return isPalenDrome;
}
add a comment |
//This c# method will check for even and odd lengh palindrome string
public static bool IsPalenDrome(string palendromeString)
{
bool isPalenDrome = false;
try
{
int halfLength = palendromeString.Length / 2;
string leftHalfString = palendromeString.Substring(0,halfLength);
char reversedArray = palendromeString.ToCharArray();
Array.Reverse(reversedArray);
string reversedString = new string(reversedArray);
string rightHalfStringReversed = reversedString.Substring(0, halfLength);
isPalenDrome = leftHalfString == rightHalfStringReversed ? true : false;
}
catch (Exception ex)
{
throw ex;
}
return isPalenDrome;
}
//This c# method will check for even and odd lengh palindrome string
public static bool IsPalenDrome(string palendromeString)
{
bool isPalenDrome = false;
try
{
int halfLength = palendromeString.Length / 2;
string leftHalfString = palendromeString.Substring(0,halfLength);
char reversedArray = palendromeString.ToCharArray();
Array.Reverse(reversedArray);
string reversedString = new string(reversedArray);
string rightHalfStringReversed = reversedString.Substring(0, halfLength);
isPalenDrome = leftHalfString == rightHalfStringReversed ? true : false;
}
catch (Exception ex)
{
throw ex;
}
return isPalenDrome;
}
edited Jul 20 '13 at 20:57
answered Jul 20 '13 at 20:48
Tabish HabibTabish Habib
213
213
add a comment |
add a comment |
This way is both concise in appearance & processes very quickly.
Func<string, bool> IsPalindrome = s => s.Reverse().Equals(s);
3
It's also dead wrong, since it will always returnfalse
.s.Reverse()
is anIEnumerable<char>
, which never compares reference-equal to the originalString
(assuming the standard definition ofEnumerable.Reverse
andObject.Equals
).
– Jeroen Mostert
Apr 13 '17 at 12:01
add a comment |
This way is both concise in appearance & processes very quickly.
Func<string, bool> IsPalindrome = s => s.Reverse().Equals(s);
3
It's also dead wrong, since it will always returnfalse
.s.Reverse()
is anIEnumerable<char>
, which never compares reference-equal to the originalString
(assuming the standard definition ofEnumerable.Reverse
andObject.Equals
).
– Jeroen Mostert
Apr 13 '17 at 12:01
add a comment |
This way is both concise in appearance & processes very quickly.
Func<string, bool> IsPalindrome = s => s.Reverse().Equals(s);
This way is both concise in appearance & processes very quickly.
Func<string, bool> IsPalindrome = s => s.Reverse().Equals(s);
answered Jun 11 '14 at 15:49
LshakaLshaka
352
352
3
It's also dead wrong, since it will always returnfalse
.s.Reverse()
is anIEnumerable<char>
, which never compares reference-equal to the originalString
(assuming the standard definition ofEnumerable.Reverse
andObject.Equals
).
– Jeroen Mostert
Apr 13 '17 at 12:01
add a comment |
3
It's also dead wrong, since it will always returnfalse
.s.Reverse()
is anIEnumerable<char>
, which never compares reference-equal to the originalString
(assuming the standard definition ofEnumerable.Reverse
andObject.Equals
).
– Jeroen Mostert
Apr 13 '17 at 12:01
3
3
It's also dead wrong, since it will always return
false
. s.Reverse()
is an IEnumerable<char>
, which never compares reference-equal to the original String
(assuming the standard definition of Enumerable.Reverse
and Object.Equals
).– Jeroen Mostert
Apr 13 '17 at 12:01
It's also dead wrong, since it will always return
false
. s.Reverse()
is an IEnumerable<char>
, which never compares reference-equal to the original String
(assuming the standard definition of Enumerable.Reverse
and Object.Equals
).– Jeroen Mostert
Apr 13 '17 at 12:01
add a comment |
String extension method, easy to use:
public static bool IsPalindrome(this string str)
{
str = new Regex("[^a-zA-Z]").Replace(str, "").ToLower();
return !str.Where((t, i) => t != str[str.Length - i - 1]).Any();
}
add a comment |
String extension method, easy to use:
public static bool IsPalindrome(this string str)
{
str = new Regex("[^a-zA-Z]").Replace(str, "").ToLower();
return !str.Where((t, i) => t != str[str.Length - i - 1]).Any();
}
add a comment |
String extension method, easy to use:
public static bool IsPalindrome(this string str)
{
str = new Regex("[^a-zA-Z]").Replace(str, "").ToLower();
return !str.Where((t, i) => t != str[str.Length - i - 1]).Any();
}
String extension method, easy to use:
public static bool IsPalindrome(this string str)
{
str = new Regex("[^a-zA-Z]").Replace(str, "").ToLower();
return !str.Where((t, i) => t != str[str.Length - i - 1]).Any();
}
edited May 10 '17 at 0:20
answered Aug 26 '16 at 22:59
Dr3Dr3
114
114
add a comment |
add a comment |
private void CheckIfPalindrome(string str)
{
//place string in array of chars
char array = str.ToCharArray();
int length = array.Length -1 ;
Boolean palindrome =true;
for (int i = 0; i <= length; i++)//go through the array
{
if (array[i] != array[length])//compare if the char in the same positions are the same eg "tattarrattat" will compare array[0]=t with array[11] =t if are not the same stop the for loop
{
MessageBox.Show("not");
palindrome = false;
break;
}
else //if they are the same make length smaller by one and do the same
{
length--;
}
}
if (palindrome) MessageBox.Show("Palindrome");
}
1
Please provide an explanation with your answer.
– Winter
Jul 11 '17 at 13:39
Code only answers arent encouraged as they dont provide much information for future readers
– WhatsThePoint
Jul 11 '17 at 13:45
I have an explanation in the comment area what else should i explain?
– pilot13
Mar 8 '18 at 11:35
add a comment |
private void CheckIfPalindrome(string str)
{
//place string in array of chars
char array = str.ToCharArray();
int length = array.Length -1 ;
Boolean palindrome =true;
for (int i = 0; i <= length; i++)//go through the array
{
if (array[i] != array[length])//compare if the char in the same positions are the same eg "tattarrattat" will compare array[0]=t with array[11] =t if are not the same stop the for loop
{
MessageBox.Show("not");
palindrome = false;
break;
}
else //if they are the same make length smaller by one and do the same
{
length--;
}
}
if (palindrome) MessageBox.Show("Palindrome");
}
1
Please provide an explanation with your answer.
– Winter
Jul 11 '17 at 13:39
Code only answers arent encouraged as they dont provide much information for future readers
– WhatsThePoint
Jul 11 '17 at 13:45
I have an explanation in the comment area what else should i explain?
– pilot13
Mar 8 '18 at 11:35
add a comment |
private void CheckIfPalindrome(string str)
{
//place string in array of chars
char array = str.ToCharArray();
int length = array.Length -1 ;
Boolean palindrome =true;
for (int i = 0; i <= length; i++)//go through the array
{
if (array[i] != array[length])//compare if the char in the same positions are the same eg "tattarrattat" will compare array[0]=t with array[11] =t if are not the same stop the for loop
{
MessageBox.Show("not");
palindrome = false;
break;
}
else //if they are the same make length smaller by one and do the same
{
length--;
}
}
if (palindrome) MessageBox.Show("Palindrome");
}
private void CheckIfPalindrome(string str)
{
//place string in array of chars
char array = str.ToCharArray();
int length = array.Length -1 ;
Boolean palindrome =true;
for (int i = 0; i <= length; i++)//go through the array
{
if (array[i] != array[length])//compare if the char in the same positions are the same eg "tattarrattat" will compare array[0]=t with array[11] =t if are not the same stop the for loop
{
MessageBox.Show("not");
palindrome = false;
break;
}
else //if they are the same make length smaller by one and do the same
{
length--;
}
}
if (palindrome) MessageBox.Show("Palindrome");
}
edited Jul 12 '17 at 7:47
answered Jul 11 '17 at 13:26
pilot13pilot13
213
213
1
Please provide an explanation with your answer.
– Winter
Jul 11 '17 at 13:39
Code only answers arent encouraged as they dont provide much information for future readers
– WhatsThePoint
Jul 11 '17 at 13:45
I have an explanation in the comment area what else should i explain?
– pilot13
Mar 8 '18 at 11:35
add a comment |
1
Please provide an explanation with your answer.
– Winter
Jul 11 '17 at 13:39
Code only answers arent encouraged as they dont provide much information for future readers
– WhatsThePoint
Jul 11 '17 at 13:45
I have an explanation in the comment area what else should i explain?
– pilot13
Mar 8 '18 at 11:35
1
1
Please provide an explanation with your answer.
– Winter
Jul 11 '17 at 13:39
Please provide an explanation with your answer.
– Winter
Jul 11 '17 at 13:39
Code only answers arent encouraged as they dont provide much information for future readers
– WhatsThePoint
Jul 11 '17 at 13:45
Code only answers arent encouraged as they dont provide much information for future readers
– WhatsThePoint
Jul 11 '17 at 13:45
I have an explanation in the comment area what else should i explain?
– pilot13
Mar 8 '18 at 11:35
I have an explanation in the comment area what else should i explain?
– pilot13
Mar 8 '18 at 11:35
add a comment |
If you just need to detect a palindrome, you can do it with a regex, as explained here. Probably not the most efficient approach, though...
add a comment |
If you just need to detect a palindrome, you can do it with a regex, as explained here. Probably not the most efficient approach, though...
add a comment |
If you just need to detect a palindrome, you can do it with a regex, as explained here. Probably not the most efficient approach, though...
If you just need to detect a palindrome, you can do it with a regex, as explained here. Probably not the most efficient approach, though...
answered Mar 20 '12 at 16:30
Thomas LevesqueThomas Levesque
236k53494665
236k53494665
add a comment |
add a comment |
That is non-trivial, there is no built in method to do that for you, you'll have to write your own. You will need to consider what rules you would like to check, like you implicitly stated you accepted reversing of one string. Also, you missed out the middle character, is this only if odd length?
So you will have something like:
if(myString.length % 2 = 0)
{
//even
string a = myString.substring(0, myString.length / 2);
string b = myString.substring(myString.length / 2 + 1, myString.lenght/2);
if(a == b)
return true;
//Rule 1: reverse
if(a == b.reverse()) //can't remember if this is a method, if not you'll have to write that too
return true;
etc, also doing whatever you want for odd strings
add a comment |
That is non-trivial, there is no built in method to do that for you, you'll have to write your own. You will need to consider what rules you would like to check, like you implicitly stated you accepted reversing of one string. Also, you missed out the middle character, is this only if odd length?
So you will have something like:
if(myString.length % 2 = 0)
{
//even
string a = myString.substring(0, myString.length / 2);
string b = myString.substring(myString.length / 2 + 1, myString.lenght/2);
if(a == b)
return true;
//Rule 1: reverse
if(a == b.reverse()) //can't remember if this is a method, if not you'll have to write that too
return true;
etc, also doing whatever you want for odd strings
add a comment |
That is non-trivial, there is no built in method to do that for you, you'll have to write your own. You will need to consider what rules you would like to check, like you implicitly stated you accepted reversing of one string. Also, you missed out the middle character, is this only if odd length?
So you will have something like:
if(myString.length % 2 = 0)
{
//even
string a = myString.substring(0, myString.length / 2);
string b = myString.substring(myString.length / 2 + 1, myString.lenght/2);
if(a == b)
return true;
//Rule 1: reverse
if(a == b.reverse()) //can't remember if this is a method, if not you'll have to write that too
return true;
etc, also doing whatever you want for odd strings
That is non-trivial, there is no built in method to do that for you, you'll have to write your own. You will need to consider what rules you would like to check, like you implicitly stated you accepted reversing of one string. Also, you missed out the middle character, is this only if odd length?
So you will have something like:
if(myString.length % 2 = 0)
{
//even
string a = myString.substring(0, myString.length / 2);
string b = myString.substring(myString.length / 2 + 1, myString.lenght/2);
if(a == b)
return true;
//Rule 1: reverse
if(a == b.reverse()) //can't remember if this is a method, if not you'll have to write that too
return true;
etc, also doing whatever you want for odd strings
answered Mar 20 '12 at 16:33
T. KileyT. Kiley
2,3621427
2,3621427
add a comment |
add a comment |
This C# method will check for even and odd length palindrome string (Recursive Approach):
public static bool IsPalindromeResursive(int rightIndex, int leftIndex, char inputString)
{
if (rightIndex == leftIndex || rightIndex < leftIndex)
return true;
if (inputString[rightIndex] == inputString[leftIndex])
return IsPalindromeResursive(--rightIndex, ++leftIndex, inputString);
else
return false;
}
add a comment |
This C# method will check for even and odd length palindrome string (Recursive Approach):
public static bool IsPalindromeResursive(int rightIndex, int leftIndex, char inputString)
{
if (rightIndex == leftIndex || rightIndex < leftIndex)
return true;
if (inputString[rightIndex] == inputString[leftIndex])
return IsPalindromeResursive(--rightIndex, ++leftIndex, inputString);
else
return false;
}
add a comment |
This C# method will check for even and odd length palindrome string (Recursive Approach):
public static bool IsPalindromeResursive(int rightIndex, int leftIndex, char inputString)
{
if (rightIndex == leftIndex || rightIndex < leftIndex)
return true;
if (inputString[rightIndex] == inputString[leftIndex])
return IsPalindromeResursive(--rightIndex, ++leftIndex, inputString);
else
return false;
}
This C# method will check for even and odd length palindrome string (Recursive Approach):
public static bool IsPalindromeResursive(int rightIndex, int leftIndex, char inputString)
{
if (rightIndex == leftIndex || rightIndex < leftIndex)
return true;
if (inputString[rightIndex] == inputString[leftIndex])
return IsPalindromeResursive(--rightIndex, ++leftIndex, inputString);
else
return false;
}
edited Jul 20 '13 at 22:12
Lee Taylor
5,03472442
5,03472442
answered Jul 20 '13 at 21:55
Tabish HabibTabish Habib
213
213
add a comment |
add a comment |
public Boolean IsPalindrome(string value)
{
var one = value.ToList<char>();
var two = one.Reverse<char>().ToList();
return one.Equals(two);
}
add a comment |
public Boolean IsPalindrome(string value)
{
var one = value.ToList<char>();
var two = one.Reverse<char>().ToList();
return one.Equals(two);
}
add a comment |
public Boolean IsPalindrome(string value)
{
var one = value.ToList<char>();
var two = one.Reverse<char>().ToList();
return one.Equals(two);
}
public Boolean IsPalindrome(string value)
{
var one = value.ToList<char>();
var two = one.Reverse<char>().ToList();
return one.Equals(two);
}
answered Apr 23 '14 at 5:11
kennydustkennydust
8701114
8701114
add a comment |
add a comment |
protected bool CheckIfPalindrome(string text)
{
if (text != null)
{
string strToUpper = Text.ToUpper();
char toReverse = strToUpper.ToCharArray();
Array.Reverse(toReverse );
String strReverse = new String(toReverse);
if (strToUpper == toReverse)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Use this the sipmlest way.
cannot compare string to char
– wruckie
Jan 13 '15 at 0:37
add a comment |
protected bool CheckIfPalindrome(string text)
{
if (text != null)
{
string strToUpper = Text.ToUpper();
char toReverse = strToUpper.ToCharArray();
Array.Reverse(toReverse );
String strReverse = new String(toReverse);
if (strToUpper == toReverse)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Use this the sipmlest way.
cannot compare string to char
– wruckie
Jan 13 '15 at 0:37
add a comment |
protected bool CheckIfPalindrome(string text)
{
if (text != null)
{
string strToUpper = Text.ToUpper();
char toReverse = strToUpper.ToCharArray();
Array.Reverse(toReverse );
String strReverse = new String(toReverse);
if (strToUpper == toReverse)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Use this the sipmlest way.
protected bool CheckIfPalindrome(string text)
{
if (text != null)
{
string strToUpper = Text.ToUpper();
char toReverse = strToUpper.ToCharArray();
Array.Reverse(toReverse );
String strReverse = new String(toReverse);
if (strToUpper == toReverse)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Use this the sipmlest way.
answered Apr 23 '14 at 11:16
Omer KOmer K
2,98474566
2,98474566
cannot compare string to char
– wruckie
Jan 13 '15 at 0:37
add a comment |
cannot compare string to char
– wruckie
Jan 13 '15 at 0:37
cannot compare string to char
– wruckie
Jan 13 '15 at 0:37
cannot compare string to char
– wruckie
Jan 13 '15 at 0:37
add a comment |
class Program
{
static void Main(string args)
{
string s, revs = "";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
Console.WriteLine(i);
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome");
}
else
{
Console.WriteLine("String is not Palindrome");
}
Console.ReadKey();
}
}
add a comment |
class Program
{
static void Main(string args)
{
string s, revs = "";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
Console.WriteLine(i);
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome");
}
else
{
Console.WriteLine("String is not Palindrome");
}
Console.ReadKey();
}
}
add a comment |
class Program
{
static void Main(string args)
{
string s, revs = "";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
Console.WriteLine(i);
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome");
}
else
{
Console.WriteLine("String is not Palindrome");
}
Console.ReadKey();
}
}
class Program
{
static void Main(string args)
{
string s, revs = "";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
Console.WriteLine(i);
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome");
}
else
{
Console.WriteLine("String is not Palindrome");
}
Console.ReadKey();
}
}
answered Sep 13 '14 at 8:07
jeetendra singh negijeetendra singh negi
1
1
add a comment |
add a comment |
public bool IsPalindroom(string input)
{
input = input.ToLower();
var loops = input.Length / 2;
var higherBoundIdx = input.Length - 1;
for (var lowerBoundIdx = 0; lowerBoundIdx < loops; lowerBoundIdx++, higherBoundIdx--)
{
if (input[lowerBoundIdx] != input[higherBoundIdx])
return false;
}
return true;
}
add a comment |
public bool IsPalindroom(string input)
{
input = input.ToLower();
var loops = input.Length / 2;
var higherBoundIdx = input.Length - 1;
for (var lowerBoundIdx = 0; lowerBoundIdx < loops; lowerBoundIdx++, higherBoundIdx--)
{
if (input[lowerBoundIdx] != input[higherBoundIdx])
return false;
}
return true;
}
add a comment |
public bool IsPalindroom(string input)
{
input = input.ToLower();
var loops = input.Length / 2;
var higherBoundIdx = input.Length - 1;
for (var lowerBoundIdx = 0; lowerBoundIdx < loops; lowerBoundIdx++, higherBoundIdx--)
{
if (input[lowerBoundIdx] != input[higherBoundIdx])
return false;
}
return true;
}
public bool IsPalindroom(string input)
{
input = input.ToLower();
var loops = input.Length / 2;
var higherBoundIdx = input.Length - 1;
for (var lowerBoundIdx = 0; lowerBoundIdx < loops; lowerBoundIdx++, higherBoundIdx--)
{
if (input[lowerBoundIdx] != input[higherBoundIdx])
return false;
}
return true;
}
answered May 22 '15 at 14:18
M. MimpenM. Mimpen
9091121
9091121
add a comment |
add a comment |
public static bool IsPalindrome(string word)
{
//first reverse the string
string reversedString = new string(word.Reverse().ToArray());
return string.Compare(word, reversedString) == 0 ? true : false;
}
add a comment |
public static bool IsPalindrome(string word)
{
//first reverse the string
string reversedString = new string(word.Reverse().ToArray());
return string.Compare(word, reversedString) == 0 ? true : false;
}
add a comment |
public static bool IsPalindrome(string word)
{
//first reverse the string
string reversedString = new string(word.Reverse().ToArray());
return string.Compare(word, reversedString) == 0 ? true : false;
}
public static bool IsPalindrome(string word)
{
//first reverse the string
string reversedString = new string(word.Reverse().ToArray());
return string.Compare(word, reversedString) == 0 ? true : false;
}
answered Feb 14 '16 at 17:44
Rennish JosephRennish Joseph
3461520
3461520
add a comment |
add a comment |
Here is an absolutely simple way to do this,
- Receive the word as input into a method.
- Assign a temp variable to the original value.
- Loop through the initial word, and add the last character to the reversal that you are constructing until the inital word has no more characters.
- Now use the spare you created to hold the original value to compare to the constructed copy.
This is a nice way as u don't have to cast ints and doubles. U can just pass them to the method in their string representation by using the ToString() method.
public static bool IsPalindrome(string word)
{
string spare = word;
string reversal = null;
while (word.Length > 0)
{
reversal = string.Concat(reversal, word.LastOrDefault());
word = word.Remove(word.Length - 1);
}
return spare.Equals(reversal);
}
So from your main method,
For even and odd length strings u just pass the whole string into the method.
add a comment |
Here is an absolutely simple way to do this,
- Receive the word as input into a method.
- Assign a temp variable to the original value.
- Loop through the initial word, and add the last character to the reversal that you are constructing until the inital word has no more characters.
- Now use the spare you created to hold the original value to compare to the constructed copy.
This is a nice way as u don't have to cast ints and doubles. U can just pass them to the method in their string representation by using the ToString() method.
public static bool IsPalindrome(string word)
{
string spare = word;
string reversal = null;
while (word.Length > 0)
{
reversal = string.Concat(reversal, word.LastOrDefault());
word = word.Remove(word.Length - 1);
}
return spare.Equals(reversal);
}
So from your main method,
For even and odd length strings u just pass the whole string into the method.
add a comment |
Here is an absolutely simple way to do this,
- Receive the word as input into a method.
- Assign a temp variable to the original value.
- Loop through the initial word, and add the last character to the reversal that you are constructing until the inital word has no more characters.
- Now use the spare you created to hold the original value to compare to the constructed copy.
This is a nice way as u don't have to cast ints and doubles. U can just pass them to the method in their string representation by using the ToString() method.
public static bool IsPalindrome(string word)
{
string spare = word;
string reversal = null;
while (word.Length > 0)
{
reversal = string.Concat(reversal, word.LastOrDefault());
word = word.Remove(word.Length - 1);
}
return spare.Equals(reversal);
}
So from your main method,
For even and odd length strings u just pass the whole string into the method.
Here is an absolutely simple way to do this,
- Receive the word as input into a method.
- Assign a temp variable to the original value.
- Loop through the initial word, and add the last character to the reversal that you are constructing until the inital word has no more characters.
- Now use the spare you created to hold the original value to compare to the constructed copy.
This is a nice way as u don't have to cast ints and doubles. U can just pass them to the method in their string representation by using the ToString() method.
public static bool IsPalindrome(string word)
{
string spare = word;
string reversal = null;
while (word.Length > 0)
{
reversal = string.Concat(reversal, word.LastOrDefault());
word = word.Remove(word.Length - 1);
}
return spare.Equals(reversal);
}
So from your main method,
For even and odd length strings u just pass the whole string into the method.
answered Oct 20 '16 at 6:15
Vishav PremlallVishav Premlall
6312
6312
add a comment |
add a comment |
Out of all the solutions, below can also be tried:
public static bool IsPalindrome(string s)
{
return s == new string(s.Reverse().ToArray());
}
add a comment |
Out of all the solutions, below can also be tried:
public static bool IsPalindrome(string s)
{
return s == new string(s.Reverse().ToArray());
}
add a comment |
Out of all the solutions, below can also be tried:
public static bool IsPalindrome(string s)
{
return s == new string(s.Reverse().ToArray());
}
Out of all the solutions, below can also be tried:
public static bool IsPalindrome(string s)
{
return s == new string(s.Reverse().ToArray());
}
answered Oct 20 '16 at 7:31
SaketSaket
1,40221134
1,40221134
add a comment |
add a comment |
Since a palindrome also includes numbers, words, sentences, and any combinations of these, and should ignore punctuation and case, (See Wikipedia Article)
I propose this solution:
public class Palindrome
{
static IList<int> Allowed = new List<int> {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9',
'0'
};
private static int GetJustAllowed(string text)
{
List<int> characters = new List<int>();
foreach (var c in text)
characters.Add(c | 0x20);
return characters.Where(c => Allowed.Contains(c)).ToArray();
}
public static bool IsPalindrome(string text)
{
if(text == null || text.Length == 1)
return true;
int chars = GetJustAllowed(text);
var length = chars.Length;
while (length > 0)
if (chars[chars.Length - length] != chars[--length])
return false;
return true;
}
public static bool IsPalindrome(int number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(double number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(decimal number)
{
return IsPalindrome(number.ToString());
}
}
The allowed list should certainly be extended to include more languages and concepts than just English, but I hope to illustrate that just checking if string.reverse() == string is not a complete solution to the palindrome problem.
– Espen
Oct 24 '16 at 20:05
add a comment |
Since a palindrome also includes numbers, words, sentences, and any combinations of these, and should ignore punctuation and case, (See Wikipedia Article)
I propose this solution:
public class Palindrome
{
static IList<int> Allowed = new List<int> {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9',
'0'
};
private static int GetJustAllowed(string text)
{
List<int> characters = new List<int>();
foreach (var c in text)
characters.Add(c | 0x20);
return characters.Where(c => Allowed.Contains(c)).ToArray();
}
public static bool IsPalindrome(string text)
{
if(text == null || text.Length == 1)
return true;
int chars = GetJustAllowed(text);
var length = chars.Length;
while (length > 0)
if (chars[chars.Length - length] != chars[--length])
return false;
return true;
}
public static bool IsPalindrome(int number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(double number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(decimal number)
{
return IsPalindrome(number.ToString());
}
}
The allowed list should certainly be extended to include more languages and concepts than just English, but I hope to illustrate that just checking if string.reverse() == string is not a complete solution to the palindrome problem.
– Espen
Oct 24 '16 at 20:05
add a comment |
Since a palindrome also includes numbers, words, sentences, and any combinations of these, and should ignore punctuation and case, (See Wikipedia Article)
I propose this solution:
public class Palindrome
{
static IList<int> Allowed = new List<int> {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9',
'0'
};
private static int GetJustAllowed(string text)
{
List<int> characters = new List<int>();
foreach (var c in text)
characters.Add(c | 0x20);
return characters.Where(c => Allowed.Contains(c)).ToArray();
}
public static bool IsPalindrome(string text)
{
if(text == null || text.Length == 1)
return true;
int chars = GetJustAllowed(text);
var length = chars.Length;
while (length > 0)
if (chars[chars.Length - length] != chars[--length])
return false;
return true;
}
public static bool IsPalindrome(int number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(double number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(decimal number)
{
return IsPalindrome(number.ToString());
}
}
Since a palindrome also includes numbers, words, sentences, and any combinations of these, and should ignore punctuation and case, (See Wikipedia Article)
I propose this solution:
public class Palindrome
{
static IList<int> Allowed = new List<int> {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9',
'0'
};
private static int GetJustAllowed(string text)
{
List<int> characters = new List<int>();
foreach (var c in text)
characters.Add(c | 0x20);
return characters.Where(c => Allowed.Contains(c)).ToArray();
}
public static bool IsPalindrome(string text)
{
if(text == null || text.Length == 1)
return true;
int chars = GetJustAllowed(text);
var length = chars.Length;
while (length > 0)
if (chars[chars.Length - length] != chars[--length])
return false;
return true;
}
public static bool IsPalindrome(int number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(double number)
{
return IsPalindrome(number.ToString());
}
public static bool IsPalindrome(decimal number)
{
return IsPalindrome(number.ToString());
}
}
edited Oct 24 '16 at 20:07
answered Mar 2 '16 at 22:21
EspenEspen
8631015
8631015
The allowed list should certainly be extended to include more languages and concepts than just English, but I hope to illustrate that just checking if string.reverse() == string is not a complete solution to the palindrome problem.
– Espen
Oct 24 '16 at 20:05
add a comment |
The allowed list should certainly be extended to include more languages and concepts than just English, but I hope to illustrate that just checking if string.reverse() == string is not a complete solution to the palindrome problem.
– Espen
Oct 24 '16 at 20:05
The allowed list should certainly be extended to include more languages and concepts than just English, but I hope to illustrate that just checking if string.reverse() == string is not a complete solution to the palindrome problem.
– Espen
Oct 24 '16 at 20:05
The allowed list should certainly be extended to include more languages and concepts than just English, but I hope to illustrate that just checking if string.reverse() == string is not a complete solution to the palindrome problem.
– Espen
Oct 24 '16 at 20:05
add a comment |
static void Main(string args)
{
string str, rev="";
Console.Write("Enter string");
str = Console.ReadLine();
for (int i = str.Length - 1; i >= 0; i--)
{
rev = rev + str[i];
}
if (rev == str)
Console.Write("Entered string is pallindrome");
else
Console.Write("Entered string is not pallindrome");
Console.ReadKey();
}
add a comment |
static void Main(string args)
{
string str, rev="";
Console.Write("Enter string");
str = Console.ReadLine();
for (int i = str.Length - 1; i >= 0; i--)
{
rev = rev + str[i];
}
if (rev == str)
Console.Write("Entered string is pallindrome");
else
Console.Write("Entered string is not pallindrome");
Console.ReadKey();
}
add a comment |
static void Main(string args)
{
string str, rev="";
Console.Write("Enter string");
str = Console.ReadLine();
for (int i = str.Length - 1; i >= 0; i--)
{
rev = rev + str[i];
}
if (rev == str)
Console.Write("Entered string is pallindrome");
else
Console.Write("Entered string is not pallindrome");
Console.ReadKey();
}
static void Main(string args)
{
string str, rev="";
Console.Write("Enter string");
str = Console.ReadLine();
for (int i = str.Length - 1; i >= 0; i--)
{
rev = rev + str[i];
}
if (rev == str)
Console.Write("Entered string is pallindrome");
else
Console.Write("Entered string is not pallindrome");
Console.ReadKey();
}
edited Jan 18 '17 at 13:04
smottt
2,72373039
2,72373039
answered Jan 18 '17 at 12:36
abhay chavanabhay chavan
11
11
add a comment |
add a comment |
string test = "Malayalam";
char palindrome = test.ToCharArray();
char reversestring = new char[palindrome.Count()];
for (int i = palindrome.Count() - 1; i >= 0; i--)
{
reversestring[palindrome.Count() - 1 - i] = palindrome[i];
}
string materializedString = new string(reversestring);
if (materializedString.ToLower() == test.ToLower())
{
Console.Write("Palindrome!");
}
else
{
Console.Write("Not a Palindrome!");
}
Console.Read();
add a comment |
string test = "Malayalam";
char palindrome = test.ToCharArray();
char reversestring = new char[palindrome.Count()];
for (int i = palindrome.Count() - 1; i >= 0; i--)
{
reversestring[palindrome.Count() - 1 - i] = palindrome[i];
}
string materializedString = new string(reversestring);
if (materializedString.ToLower() == test.ToLower())
{
Console.Write("Palindrome!");
}
else
{
Console.Write("Not a Palindrome!");
}
Console.Read();
add a comment |
string test = "Malayalam";
char palindrome = test.ToCharArray();
char reversestring = new char[palindrome.Count()];
for (int i = palindrome.Count() - 1; i >= 0; i--)
{
reversestring[palindrome.Count() - 1 - i] = palindrome[i];
}
string materializedString = new string(reversestring);
if (materializedString.ToLower() == test.ToLower())
{
Console.Write("Palindrome!");
}
else
{
Console.Write("Not a Palindrome!");
}
Console.Read();
string test = "Malayalam";
char palindrome = test.ToCharArray();
char reversestring = new char[palindrome.Count()];
for (int i = palindrome.Count() - 1; i >= 0; i--)
{
reversestring[palindrome.Count() - 1 - i] = palindrome[i];
}
string materializedString = new string(reversestring);
if (materializedString.ToLower() == test.ToLower())
{
Console.Write("Palindrome!");
}
else
{
Console.Write("Not a Palindrome!");
}
Console.Read();
answered Sep 13 '17 at 0:10
user3725809user3725809
11
11
add a comment |
add a comment |
public static bool palindrome(string t)
{
int i = t.Length;
for (int j = 0; j < i / 2; j++)
{
if (t[j] == t[i - j-1])
{
continue;
}
else
{
return false;
break;
}
}
return true;
}
1
Please, next time, along with the code enter a short explanation.
– Kerberos
Nov 4 '17 at 9:36
add a comment |
public static bool palindrome(string t)
{
int i = t.Length;
for (int j = 0; j < i / 2; j++)
{
if (t[j] == t[i - j-1])
{
continue;
}
else
{
return false;
break;
}
}
return true;
}
1
Please, next time, along with the code enter a short explanation.
– Kerberos
Nov 4 '17 at 9:36
add a comment |
public static bool palindrome(string t)
{
int i = t.Length;
for (int j = 0; j < i / 2; j++)
{
if (t[j] == t[i - j-1])
{
continue;
}
else
{
return false;
break;
}
}
return true;
}
public static bool palindrome(string t)
{
int i = t.Length;
for (int j = 0; j < i / 2; j++)
{
if (t[j] == t[i - j-1])
{
continue;
}
else
{
return false;
break;
}
}
return true;
}
answered Nov 4 '17 at 9:23
Artyom ArshakyanArtyom Arshakyan
11
11
1
Please, next time, along with the code enter a short explanation.
– Kerberos
Nov 4 '17 at 9:36
add a comment |
1
Please, next time, along with the code enter a short explanation.
– Kerberos
Nov 4 '17 at 9:36
1
1
Please, next time, along with the code enter a short explanation.
– Kerberos
Nov 4 '17 at 9:36
Please, next time, along with the code enter a short explanation.
– Kerberos
Nov 4 '17 at 9:36
add a comment |
use this way from dotnetperls
using System;
class Program
{
/// <summary>
/// Determines whether the string is a palindrome.
/// </summary>
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
// Scan forward for a while invalid.
while (!char.IsLetterOrDigit(a))
{
min++;
if (min > max)
{
return true;
}
a = value[min];
}
// Scan backward for b while invalid.
while (!char.IsLetterOrDigit(b))
{
max--;
if (min > max)
{
return true;
}
b = value[max];
}
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
static void Main()
{
string array =
{
"A man, a plan, a canal: Panama.",
"A Toyota. Race fast, safe car. A Toyota.",
"Cigar? Toss it in a can. It is so tragic.",
"Dammit, I'm mad!",
"Delia saw I was ailed.",
"Desserts, I stressed!",
"Draw, O coward!",
"Lepers repel.",
"Live not on evil.",
"Lonely Tylenol.",
"Murder for a jar of red rum.",
"Never odd or even.",
"No lemon, no melon.",
"Senile felines.",
"So many dynamos!",
"Step on no pets.",
"Was it a car or a cat I saw?",
"Dot Net Perls is not a palindrome.",
"Why are you reading this?",
"This article is not useful.",
"...",
"...Test"
};
foreach (string value in array)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
}
}
Please add description to your code explaining what it does
– Rahul Gupta
Dec 22 '17 at 11:28
see the comment at the top of IsPalindrome function :)
– FAREH
Dec 22 '17 at 13:05
add a comment |
use this way from dotnetperls
using System;
class Program
{
/// <summary>
/// Determines whether the string is a palindrome.
/// </summary>
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
// Scan forward for a while invalid.
while (!char.IsLetterOrDigit(a))
{
min++;
if (min > max)
{
return true;
}
a = value[min];
}
// Scan backward for b while invalid.
while (!char.IsLetterOrDigit(b))
{
max--;
if (min > max)
{
return true;
}
b = value[max];
}
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
static void Main()
{
string array =
{
"A man, a plan, a canal: Panama.",
"A Toyota. Race fast, safe car. A Toyota.",
"Cigar? Toss it in a can. It is so tragic.",
"Dammit, I'm mad!",
"Delia saw I was ailed.",
"Desserts, I stressed!",
"Draw, O coward!",
"Lepers repel.",
"Live not on evil.",
"Lonely Tylenol.",
"Murder for a jar of red rum.",
"Never odd or even.",
"No lemon, no melon.",
"Senile felines.",
"So many dynamos!",
"Step on no pets.",
"Was it a car or a cat I saw?",
"Dot Net Perls is not a palindrome.",
"Why are you reading this?",
"This article is not useful.",
"...",
"...Test"
};
foreach (string value in array)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
}
}
Please add description to your code explaining what it does
– Rahul Gupta
Dec 22 '17 at 11:28
see the comment at the top of IsPalindrome function :)
– FAREH
Dec 22 '17 at 13:05
add a comment |
use this way from dotnetperls
using System;
class Program
{
/// <summary>
/// Determines whether the string is a palindrome.
/// </summary>
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
// Scan forward for a while invalid.
while (!char.IsLetterOrDigit(a))
{
min++;
if (min > max)
{
return true;
}
a = value[min];
}
// Scan backward for b while invalid.
while (!char.IsLetterOrDigit(b))
{
max--;
if (min > max)
{
return true;
}
b = value[max];
}
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
static void Main()
{
string array =
{
"A man, a plan, a canal: Panama.",
"A Toyota. Race fast, safe car. A Toyota.",
"Cigar? Toss it in a can. It is so tragic.",
"Dammit, I'm mad!",
"Delia saw I was ailed.",
"Desserts, I stressed!",
"Draw, O coward!",
"Lepers repel.",
"Live not on evil.",
"Lonely Tylenol.",
"Murder for a jar of red rum.",
"Never odd or even.",
"No lemon, no melon.",
"Senile felines.",
"So many dynamos!",
"Step on no pets.",
"Was it a car or a cat I saw?",
"Dot Net Perls is not a palindrome.",
"Why are you reading this?",
"This article is not useful.",
"...",
"...Test"
};
foreach (string value in array)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
}
}
use this way from dotnetperls
using System;
class Program
{
/// <summary>
/// Determines whether the string is a palindrome.
/// </summary>
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
// Scan forward for a while invalid.
while (!char.IsLetterOrDigit(a))
{
min++;
if (min > max)
{
return true;
}
a = value[min];
}
// Scan backward for b while invalid.
while (!char.IsLetterOrDigit(b))
{
max--;
if (min > max)
{
return true;
}
b = value[max];
}
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
static void Main()
{
string array =
{
"A man, a plan, a canal: Panama.",
"A Toyota. Race fast, safe car. A Toyota.",
"Cigar? Toss it in a can. It is so tragic.",
"Dammit, I'm mad!",
"Delia saw I was ailed.",
"Desserts, I stressed!",
"Draw, O coward!",
"Lepers repel.",
"Live not on evil.",
"Lonely Tylenol.",
"Murder for a jar of red rum.",
"Never odd or even.",
"No lemon, no melon.",
"Senile felines.",
"So many dynamos!",
"Step on no pets.",
"Was it a car or a cat I saw?",
"Dot Net Perls is not a palindrome.",
"Why are you reading this?",
"This article is not useful.",
"...",
"...Test"
};
foreach (string value in array)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
}
}
edited Dec 22 '17 at 13:02
answered Dec 22 '17 at 11:05
FAREHFAREH
216
216
Please add description to your code explaining what it does
– Rahul Gupta
Dec 22 '17 at 11:28
see the comment at the top of IsPalindrome function :)
– FAREH
Dec 22 '17 at 13:05
add a comment |
Please add description to your code explaining what it does
– Rahul Gupta
Dec 22 '17 at 11:28
see the comment at the top of IsPalindrome function :)
– FAREH
Dec 22 '17 at 13:05
Please add description to your code explaining what it does
– Rahul Gupta
Dec 22 '17 at 11:28
Please add description to your code explaining what it does
– Rahul Gupta
Dec 22 '17 at 11:28
see the comment at the top of IsPalindrome function :)
– FAREH
Dec 22 '17 at 13:05
see the comment at the top of IsPalindrome function :)
– FAREH
Dec 22 '17 at 13:05
add a comment |
This is a short and efficient way of checking palindrome.
bool checkPalindrome(string inputString) {
int length = inputString.Length;
for(int i = 0; i < length/2; i++){
if(inputString[i] != inputString[length-1-i]){
return false;
}
}
return true;
}
add a comment |
This is a short and efficient way of checking palindrome.
bool checkPalindrome(string inputString) {
int length = inputString.Length;
for(int i = 0; i < length/2; i++){
if(inputString[i] != inputString[length-1-i]){
return false;
}
}
return true;
}
add a comment |
This is a short and efficient way of checking palindrome.
bool checkPalindrome(string inputString) {
int length = inputString.Length;
for(int i = 0; i < length/2; i++){
if(inputString[i] != inputString[length-1-i]){
return false;
}
}
return true;
}
This is a short and efficient way of checking palindrome.
bool checkPalindrome(string inputString) {
int length = inputString.Length;
for(int i = 0; i < length/2; i++){
if(inputString[i] != inputString[length-1-i]){
return false;
}
}
return true;
}
edited Jul 17 '18 at 20:45
Kzrystof
2,00431321
2,00431321
answered Jul 17 '18 at 20:13
Amrit SubediAmrit Subedi
11
11
add a comment |
add a comment |
public bool Solution(string content)
{
int length = content.Length;
int half = length/2;
int isOddLength = length%2;
// Counter for checking the string from the middle
int j = (isOddLength==0) ? half:half+1;
for(int i=half-1;i>=0;i--)
{
if(content[i] != content[j])
{
return false;
}
j++;
}
return true;
}
Bonus points if you add some details on how this works for future visitors. A couple of test cases, too, maybe?
– Alexander van Oostenrijk
Aug 7 '18 at 20:08
add a comment |
public bool Solution(string content)
{
int length = content.Length;
int half = length/2;
int isOddLength = length%2;
// Counter for checking the string from the middle
int j = (isOddLength==0) ? half:half+1;
for(int i=half-1;i>=0;i--)
{
if(content[i] != content[j])
{
return false;
}
j++;
}
return true;
}
Bonus points if you add some details on how this works for future visitors. A couple of test cases, too, maybe?
– Alexander van Oostenrijk
Aug 7 '18 at 20:08
add a comment |
public bool Solution(string content)
{
int length = content.Length;
int half = length/2;
int isOddLength = length%2;
// Counter for checking the string from the middle
int j = (isOddLength==0) ? half:half+1;
for(int i=half-1;i>=0;i--)
{
if(content[i] != content[j])
{
return false;
}
j++;
}
return true;
}
public bool Solution(string content)
{
int length = content.Length;
int half = length/2;
int isOddLength = length%2;
// Counter for checking the string from the middle
int j = (isOddLength==0) ? half:half+1;
for(int i=half-1;i>=0;i--)
{
if(content[i] != content[j])
{
return false;
}
j++;
}
return true;
}
answered Aug 7 '18 at 19:39
Tanvir AnowarTanvir Anowar
456
456
Bonus points if you add some details on how this works for future visitors. A couple of test cases, too, maybe?
– Alexander van Oostenrijk
Aug 7 '18 at 20:08
add a comment |
Bonus points if you add some details on how this works for future visitors. A couple of test cases, too, maybe?
– Alexander van Oostenrijk
Aug 7 '18 at 20:08
Bonus points if you add some details on how this works for future visitors. A couple of test cases, too, maybe?
– Alexander van Oostenrijk
Aug 7 '18 at 20:08
Bonus points if you add some details on how this works for future visitors. A couple of test cases, too, maybe?
– Alexander van Oostenrijk
Aug 7 '18 at 20:08
add a comment |
public bool MojTestPalindrome (string word)
{
bool yes = false;
chartest1 = word.ToArray();
char test2 = test1.Reverse().ToArray();
for (int i=0; i< test2.Length; i++)
{
if (test1[i] != test2[test2.Length - 1 - i])
{
yes = false;
break;
}
else {
yes = true;
}
}
if (yes == true)
{
return true;
}
else
return false;
}
add a comment |
public bool MojTestPalindrome (string word)
{
bool yes = false;
chartest1 = word.ToArray();
char test2 = test1.Reverse().ToArray();
for (int i=0; i< test2.Length; i++)
{
if (test1[i] != test2[test2.Length - 1 - i])
{
yes = false;
break;
}
else {
yes = true;
}
}
if (yes == true)
{
return true;
}
else
return false;
}
add a comment |
public bool MojTestPalindrome (string word)
{
bool yes = false;
chartest1 = word.ToArray();
char test2 = test1.Reverse().ToArray();
for (int i=0; i< test2.Length; i++)
{
if (test1[i] != test2[test2.Length - 1 - i])
{
yes = false;
break;
}
else {
yes = true;
}
}
if (yes == true)
{
return true;
}
else
return false;
}
public bool MojTestPalindrome (string word)
{
bool yes = false;
chartest1 = word.ToArray();
char test2 = test1.Reverse().ToArray();
for (int i=0; i< test2.Length; i++)
{
if (test1[i] != test2[test2.Length - 1 - i])
{
yes = false;
break;
}
else {
yes = true;
}
}
if (yes == true)
{
return true;
}
else
return false;
}
answered Jan 30 at 17:47
Jerko ViskovJerko Viskov
1
1
add a comment |
add a comment |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class palindrome
{
static void Main(string args)
{
Console.Write("Enter a number:");
string panstring = Console.ReadLine();
Palindrome(panstring);
Console.ReadKey();
}
static int index = 0;
public static void Palindrome(string strexcluding)
{
try
{
string reversecounter = string.Empty;
for (int i = strexcluding.Length - 1; i >= 0; i--)
{
if (strexcluding[i].ToString() != null)
reversecounter += strexcluding[i].ToString();
}
if (reversecounter == strexcluding)
{
Console.WriteLine("Palindrome Number: " + strexcluding);
}
else
{
Sum(strexcluding);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void Sum(string stringnumber)
{
try
{
index++;
string number1 = stringnumber;
string number2 = stringnumber;
string array = new string[number1.Length];
string obtained = string.Empty;
string sreverse = null;
Console.WriteLine(index + ".step : " + number1 + "+" + number2);
for (int i = 0; i < number1.Length; i++)
{
int temp1 = Convert.ToInt32(number1[number1.Length - i - 1].ToString());
int temp2 = Convert.ToInt32(number2[number2.Length - i - 1].ToString());
if (temp1 + temp2 >= 10)
{
if (number1.Length - 1 == number1.Length - 1 - i)
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
else if (number1.Length - 1 == i)
{
if (obtained == "one")
{
array[i] = (temp1 + temp2 + 1).ToString();
}
else
{
array[i] = (temp1 + temp2).ToString();
}
}
else
{
if (obtained == "one")
{
array[i] = ((temp1 + temp2 + 1) - 10).ToString();
}
else
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
}
}
else
{
if (obtained == "one")
array[i] = (temp1 + temp2 + 1).ToString();
else
array[i] = (temp1 + temp2).ToString();
obtained = "Zero";
}
}
for (int i = array.Length - 1; i >= 0; i--)
{
if (array[i] != null)
sreverse += array[i].ToString();
}
Palindrome(sreverse);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
add a comment |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class palindrome
{
static void Main(string args)
{
Console.Write("Enter a number:");
string panstring = Console.ReadLine();
Palindrome(panstring);
Console.ReadKey();
}
static int index = 0;
public static void Palindrome(string strexcluding)
{
try
{
string reversecounter = string.Empty;
for (int i = strexcluding.Length - 1; i >= 0; i--)
{
if (strexcluding[i].ToString() != null)
reversecounter += strexcluding[i].ToString();
}
if (reversecounter == strexcluding)
{
Console.WriteLine("Palindrome Number: " + strexcluding);
}
else
{
Sum(strexcluding);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void Sum(string stringnumber)
{
try
{
index++;
string number1 = stringnumber;
string number2 = stringnumber;
string array = new string[number1.Length];
string obtained = string.Empty;
string sreverse = null;
Console.WriteLine(index + ".step : " + number1 + "+" + number2);
for (int i = 0; i < number1.Length; i++)
{
int temp1 = Convert.ToInt32(number1[number1.Length - i - 1].ToString());
int temp2 = Convert.ToInt32(number2[number2.Length - i - 1].ToString());
if (temp1 + temp2 >= 10)
{
if (number1.Length - 1 == number1.Length - 1 - i)
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
else if (number1.Length - 1 == i)
{
if (obtained == "one")
{
array[i] = (temp1 + temp2 + 1).ToString();
}
else
{
array[i] = (temp1 + temp2).ToString();
}
}
else
{
if (obtained == "one")
{
array[i] = ((temp1 + temp2 + 1) - 10).ToString();
}
else
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
}
}
else
{
if (obtained == "one")
array[i] = (temp1 + temp2 + 1).ToString();
else
array[i] = (temp1 + temp2).ToString();
obtained = "Zero";
}
}
for (int i = array.Length - 1; i >= 0; i--)
{
if (array[i] != null)
sreverse += array[i].ToString();
}
Palindrome(sreverse);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
add a comment |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class palindrome
{
static void Main(string args)
{
Console.Write("Enter a number:");
string panstring = Console.ReadLine();
Palindrome(panstring);
Console.ReadKey();
}
static int index = 0;
public static void Palindrome(string strexcluding)
{
try
{
string reversecounter = string.Empty;
for (int i = strexcluding.Length - 1; i >= 0; i--)
{
if (strexcluding[i].ToString() != null)
reversecounter += strexcluding[i].ToString();
}
if (reversecounter == strexcluding)
{
Console.WriteLine("Palindrome Number: " + strexcluding);
}
else
{
Sum(strexcluding);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void Sum(string stringnumber)
{
try
{
index++;
string number1 = stringnumber;
string number2 = stringnumber;
string array = new string[number1.Length];
string obtained = string.Empty;
string sreverse = null;
Console.WriteLine(index + ".step : " + number1 + "+" + number2);
for (int i = 0; i < number1.Length; i++)
{
int temp1 = Convert.ToInt32(number1[number1.Length - i - 1].ToString());
int temp2 = Convert.ToInt32(number2[number2.Length - i - 1].ToString());
if (temp1 + temp2 >= 10)
{
if (number1.Length - 1 == number1.Length - 1 - i)
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
else if (number1.Length - 1 == i)
{
if (obtained == "one")
{
array[i] = (temp1 + temp2 + 1).ToString();
}
else
{
array[i] = (temp1 + temp2).ToString();
}
}
else
{
if (obtained == "one")
{
array[i] = ((temp1 + temp2 + 1) - 10).ToString();
}
else
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
}
}
else
{
if (obtained == "one")
array[i] = (temp1 + temp2 + 1).ToString();
else
array[i] = (temp1 + temp2).ToString();
obtained = "Zero";
}
}
for (int i = array.Length - 1; i >= 0; i--)
{
if (array[i] != null)
sreverse += array[i].ToString();
}
Palindrome(sreverse);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class palindrome
{
static void Main(string args)
{
Console.Write("Enter a number:");
string panstring = Console.ReadLine();
Palindrome(panstring);
Console.ReadKey();
}
static int index = 0;
public static void Palindrome(string strexcluding)
{
try
{
string reversecounter = string.Empty;
for (int i = strexcluding.Length - 1; i >= 0; i--)
{
if (strexcluding[i].ToString() != null)
reversecounter += strexcluding[i].ToString();
}
if (reversecounter == strexcluding)
{
Console.WriteLine("Palindrome Number: " + strexcluding);
}
else
{
Sum(strexcluding);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void Sum(string stringnumber)
{
try
{
index++;
string number1 = stringnumber;
string number2 = stringnumber;
string array = new string[number1.Length];
string obtained = string.Empty;
string sreverse = null;
Console.WriteLine(index + ".step : " + number1 + "+" + number2);
for (int i = 0; i < number1.Length; i++)
{
int temp1 = Convert.ToInt32(number1[number1.Length - i - 1].ToString());
int temp2 = Convert.ToInt32(number2[number2.Length - i - 1].ToString());
if (temp1 + temp2 >= 10)
{
if (number1.Length - 1 == number1.Length - 1 - i)
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
else if (number1.Length - 1 == i)
{
if (obtained == "one")
{
array[i] = (temp1 + temp2 + 1).ToString();
}
else
{
array[i] = (temp1 + temp2).ToString();
}
}
else
{
if (obtained == "one")
{
array[i] = ((temp1 + temp2 + 1) - 10).ToString();
}
else
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
}
}
else
{
if (obtained == "one")
array[i] = (temp1 + temp2 + 1).ToString();
else
array[i] = (temp1 + temp2).ToString();
obtained = "Zero";
}
}
for (int i = array.Length - 1; i >= 0; i--)
{
if (array[i] != null)
sreverse += array[i].ToString();
}
Palindrome(sreverse);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
edited Jan 11 '18 at 8:04
answered Jan 9 '18 at 13:32
Zekeriya Ömür TorunoğluZekeriya Ömür Torunoğlu
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9790749%2fcheck-if-a-string-is-a-palindrome%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's your actual problem here? Just use
myString.SubString()
.– ken2k
Mar 20 '12 at 16:23
@ken2k how to break a string into two equal substring.
– ankur
Mar 20 '12 at 16:24
1
So you just want to check if the string is an anagram?
– Thomas Levesque
Mar 20 '12 at 16:25
@ThomasLevesque yup.
– ankur
Mar 20 '12 at 16:26
3
Or just a palindrome?
– cadrell0
Mar 20 '12 at 16:27