ads

Monday, September 3, 2007

C# Sample Code : Exercise Palindrome

Filename: Program.CS



using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_02___Palindromes
{
class Program
{
//string aword;

static void Main(string[] args)
{
bool wResult;

Console.WriteLine("Palindrome Checker");


for (int x = 0; x < 20; x++)
{
Console.Write("=");
}
Console.WriteLine();
Console.Write("Input String : ");
string aword = Console.ReadLine();

clsCheckWord CheckWord = new clsCheckWord();

CheckWord.Givenword = aword;
int ix = CheckWord.isPalindrome();

if (ix == 1)
{
Console.Write("Result : A Valid Palindrome");
}
else
{
Console.Write("Result : Not A Palindrome");
}

Console.ReadLine();
}
}
}




Class Filename : clsCheckWord.cs




using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise_02___Palindromes
{
class clsCheckWord
{
public clsCheckWord(){}

public string givenword="";

public string Givenword
{
get { return givenword; }
set { givenword = value; }
}

public int isPalindrome()
{
string tmpcomp="";

for (int x = 0; x < givenword.Length; x++)
{
tmpcomp = givenword.Substring(x, 1) + tmpcomp;
}

if (tmpcomp == givenword)
{
return 1;
}
else
{
return 0;
}
}
}
}

18 comments:

Anonymous said...

As an interviewer I give this question to mid-level .net developers. This solution only scratches the surface, however, and from here the question gets a bit harder. For instance, I want to send in an entire paragraph and find all palindromes that are three chars long or higher. This includes ignoring the whitespace and punctuation when searching for the palindrome, but preserving it for when we display the palindrome. For example, I would want 'rise to vote sir.' to return exactly as it is, not as 'risetovotesir.'. But obviously the spaces and the period at the end would make the paindrome checker miss this if they weren't being ignored by the comparer. Also, the way this code was written, it will not catch palindromes that have double letters at thier centers, ie: 'beeb' (not actually a word, but still a palindorme).
I don't think I would give a hire based on this solution, come to think of it. You should try it again, traversing the string and checking values on either side of the current character as you do so (you are only checking in front of the current char). After all, the real way to find a palindrome is to start in its center and work out from there. This involves a loop on the string and, for each character, a check on its forward neighbor and backwards neighbor to see if those are equal.
In your isPalindrome method, use StringBuilder instead of string when building up your comparison string since the type 'string' is immutable, you are creating a new copy of that string in memory every time you modify it rather than the stringbuilder class which just updates the existing object. Also, for God's sake add some error handling to keep form puking on error. For instance, your current app crashes if you send in a one character string when you try givenWord.Substring(x, 1) with an indexoutOfRangeException. Either check the length, or at least have a try/catch block somewhere to ensure the app doesn't blow up. That's about it. Hope I was constructive and not just sounding like a jerk.

Max said...

Hello
I'm a beginner could you explain to me what this segment do
public string Givenword
{
get
{
return givenword;
}
set
{
givenword = value;
}

Sara Nacole said...

yeah thats a property for a class instance variable, type string. It gets the value givenword (from a constructor) and sets givenword equal to whatever value you pass into the constructor argument

Anonymous said...

Hi girl. I am only I start to study c language program.
I have visited a site http://program.v3host.be/?cat=204.
Much to our regret I have very little found out about it.
You can probably tell to me a good site about arrays c program.
I will be very glad.
Thansk

Anonymous said...

Greetings mans. I am only I start to study c program, c program for loop.
I have visited a site http://program.v3host.be/?cat=204.
Much to our regret I have very little found out about it.
You can probably tell to me a good site about c program array.
I will be very glad.
Thansk

Anonymous said...

Greetings man. I am only I start to studyexercise program
I have visited a site http://program.v3host.be/?cat=83.
Much to our regret I have very little found out about it.
You can probably tell to me a good site about weekly exercise program.
I will be very glad.
Thansk

Anonymous said...

Thanks for ones marvelous posting! I definitely enjoyed reading
it, you might be a great author.I will always bookmark your blog and may come
back later on. I want to encourage you to continue your
great work, have a nice afternoon!
My blog : van nelle halfzware

Anonymous said...

You need to take part in a contest for one of
the best blogs on the web. I'm going to recommend this blog!
My webpage : amber leaf

Anonymous said...

When I initially commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get several emails with the same comment.
Is there any way you can remove people from that service?
Many thanks!
Also see my website: borkum riff cherry

Anonymous said...

An interesting discussion is definitely worth comment.

I think that you should write more about this issue, it
might not be a taboo matter but generally people do not speak about these topics.
To the next! Many thanks!!
Look into my blog post ... http://come2myworld.com/

Anonymous said...

I love it when people get together and share thoughts.
Great blog, stick with it!

Feel free to surf to my site - Workouts to jump higher

Anonymous said...

Great items from you, man. I've take note your stuff prior to and you're
just extremely great. I actually like what you've obtained here, really like what you're saying and the best way during which you are saying it.
You make it enjoyable and you still care for to keep it
sensible. I can not wait to learn far more from you.
That is really a great website.

Check out my blog post - Vertical Jump Workouts

Anonymous said...

Heya! I just wanted to ask if you ever have any trouble with hackers?

My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due
to no back up. Do you have any solutions to protect against hackers?


Here is my page - Jump High To Dunk

Anonymous said...

Very good article! We will be linking to this particularly great content on our website.
Keep up the great writing.

Also visit my webpage exercises to increase vertical jump

Anonymous said...

I do believe all the concepts you've offered for your post. They're very convincing
and can certainly work. Nonetheless, the posts are too short for starters.

May you please extend them a bit from next time? Thanks for the post.


my web page rede.nectar.org.br

Anonymous said...

Simply want to say your article is as surprising.
The clearness in your post is just nice and i could assume you're an expert on this subject. Well with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please keep up the rewarding work.

my page; vortext.org

Anonymous said...

With havin so much content do you ever run into any issues of plagorism
or copyright infringement? My site has a lot of unique content I've either created myself or outsourced but it looks like a lot of it is popping it up all over the internet without my authorization. Do you know any methods to help prevent content from being ripped off? I'd certainly appreciate it.


Feel free to surf to my web site ... exercises to jump higher exercises to improve vertical exercises to improve vertical jump exercises to improve vertical leap exercises to increase vertical exercises to increase vertical jump exercises to increase vertical leap exercises for vertical exercises for vertical jump exercises For vertical leap workouts to jump higher Workouts to improve vertical workouts to improve vertical jump workouts to improve vertical leap workouts to increase vertical workouts to increase vertical jump workouts to increase vertical leap workouts for vertical workouts for vertical jump workouts for vertical leap vertical jump exercises vertical leap exercises Vertical jump workouts vertical leap workouts

Anonymous said...

The program comes with full warranty 60 days money back, so everyone can actually try this muscle building system for a few
weeks and see the results without any risk at all.
To find out if it is actually true and to understand better if Kyle Leon's system is generally for you or not, let's look into a few of the benefits and
drawbacks of the product. Who is this Somanabolic
Muscle Maximizer Training Software Designed For.

Look at my weblog: muscle maximizer results

Google Search