I want to check weather the given string is palindrome or not .. i am getting ok for single word string like "sunus" but getting compile time errors while doing with big ones ? like "a but tuba" ??
Any solution manually appraoch plz
TY
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 16, 2013, 5:22 PM
See if your code can deal with this palindrome:
"Doc, note: I dissent. A fast never prevents a fatness. I diet on cod."
My previous version didn't deal with capitalization as I didn't think that was needed but I've adjusted it now so that it does, again without using any string function:
using System;
using System.Text;
class Test
{
static void Main()
{
string[] words =
{
"sunus",
"a but tuba",
"Rotor",
"raja",
"Able was I ere I saw Elba",
"vulpes",
"Doc, note: I dissent. A fast never prevents a fatness. I diet on cod."
};
var sb = new StringBuilder();
for(int i = 0; i < words.Length; i++)
{
// remove everything except letters from word
foreach(char c in words[i])
{
if (c >= 97 && c <= 122)
sb.Append(c);
else if (c >= 65 && c <= 90)
sb.Append((char)(c + 32));
}
string temp = sb.ToString();
sb.Length = 0;
// now reverse the word
for(int j = temp.Length - 1; j >= 0; j--)
{
sb.Append(temp[j]);
}
if (temp == sb.ToString())
{
Console.WriteLine("'{0}' is a palindrome", words[i]);
}
else
{
Console.WriteLine("'{0}' is not a palindrome", words[i]);
}
sb.Length = 0;
}
Console.ReadKey();
}
}
However, here's an improved way of doing it which doesn't reverse the string but iterates it from both ends at once, comparing characters as it goes:
using System;
class Test
{
static void Main()
{
string[] words =
{
"sunus",
"a but tuba",
"Rotor",
"raja",
"Able was I ere I saw Elba",
"vulpes",
"Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.",
"Madam, I'm Eve"
};
for(int i = 0; i < words.Length; i++)
{
string w = words[i].ToLower();
bool isPalindrome = true;
for(int j = 0, k = w.Length - 1; j <= k; j++, k--)
{
if (Char.IsLetter(w[j]))
{
if (char.IsLetter(w[k]))
{
if (w[j] != w[k])
{
isPalindrome = false;
break;
}
}
else
{
j--;
}
}
else if (char.IsLetter(w[k]))
{
k++;
}
}
if (isPalindrome)
{
Console.WriteLine("'{0}' is a palindrome", words[i]);
}
else
{
Console.WriteLine("'{0}' is not a palindrome", words[i]);
}
}
Console.ReadKey();
}
}
SUNIL GUTTAPosted Jul 17, 2013, 5:16 AM
Well your above manual code is just fantastic :) just felt the taste of programming language its gives :)
Ty Vulpes & everyone for support
VulpesPosted Jul 17, 2013, 4:46 AM
Yes, you can allow for capitalization in the way you suggested (i.e. using ToLower) but, as I'd not used any functions in my original version, I wanted to do the same in the updated version :)
The Wikipedia article on palindromes is worth reading:
http://en.wikipedia.org/wiki/Palindrome
It has some more good examples such as:
"Eva, can I stab bats in a cave?"
"Rise to vote, sir"
"Dammit, I'm mad!"
SUNIL GUTTAPosted Jul 17, 2013, 4:27 AM
Well this is i have this simple string like " PL-ATITAL P " saying it as not a pallondrome but thing we we all know it is ...
+ acc to the methods provided by respectively the output will be wen we reverse like
PLATITA-LP which is not equal to PL-ATITALP beocz of that ' - ' ... please once can you cross check with the given string and ping back :) ty ty
thank you :)
SUNIL GUTTAPosted Jul 17, 2013, 4:11 AM
I think it can be done with small modification in your Foreach loop i.e foreach(char c in words[i])
when i changed ur foreach loop into something like this its working good for everything ..!!
-------------------- foreach(char c in words[i].ToLower())-----------------
I this this can be done right vulpes :)
Sanjeeb LenkaPosted Jul 16, 2013, 4:44 PM
Hemant SrivastavaPosted Jul 16, 2013, 4:36 PM
just clarifying your doubt: "Hemant Srivastava well just awesome Ty but when pallamdrom contains non special chars(comma,&,# ,',...) like " a but, tub'a " may fail"
The code which I provided would work with special characters too..
SUNIL GUTTAPosted Jul 16, 2013, 4:13 PM
Well but Sanjeeb Lenka your solution just superb but only thing is . It is applicable to single word strings but it will prove to be wrong while we consider "a but tuba" this one . I tried
Hemant Srivastava well just awesome Ty but when pallamdrom contains non special chars(comma,&,# ,',...) like " a but, tub'a " may fail
TO every possible mistake . MR.Vulpes code mentioned above is best of sorts once check it
TY very much for all ur whole hearted support to forum & people like me
VulpesPosted Jul 16, 2013, 3:45 PM
http://www.c-sharpcorner.com/Blogs/8791/using-linq-to-check-whether-a-string-is-a-pangram-or-a-palin.aspx
Hemant SrivastavaPosted Jul 16, 2013, 3:40 PM
private string CheckPalinDrome(string text)
{
//Converting into Upper case and remove blank spaces in between
text = textBox2.Text.ToUpper().Replace(" ", string.Empty);
string revText = ReverseString(text);
if (revText == text)
{
return("palindrome");
}
else
{
return ("Not palindrome");
}
}
private string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
Sanjeeb LenkaPosted Jul 16, 2013, 3:29 PM
http://www.dotnetperls.com/stringcomparison
SUNIL GUTTAPosted Jul 16, 2013, 3:25 PM
Sanjeeb Lenka ty for your reply to :) its way to complex to understand for a guy like me
StringComparison.OrdinalIgnoreCase ??? please can u tell me what is it work & ty friend
Sanjeeb LenkaPosted Jul 16, 2013, 3:18 PM
using System;
class Program
{
static void Main(string[] args)
{
string str, revstr;
Console.WriteLine("Enter Any String to Know It is Palindrome or not");
str = Console.ReadLine();
char[] tempstr = str.ToCharArray();
Array.Reverse(tempstr);
revstr = new string(tempstr);
bool caseignore = str.Equals(revstr, StringComparison.OrdinalIgnoreCase);
if (caseignore == true)
{
Console.WriteLine("............" + str + " Is a Palindrome..........");
}
else
{
Console.WriteLine("............" + str + " Is Not a Palindrome........");
}
Console.Read();
}
}
VulpesPosted Jul 16, 2013, 2:59 PM