RSS Forums RSS
Please support our C++ advertiser: Programming Forums

Please help me with arrays

Join Date: Jul 2005
Posts: 1,344
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 182
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: Please help me with arrays

  #10  
Jul 28th, 2005
You can check for palindrome(ness) using many different techniques. You could reverse the entire string and compare the original string with the reverse to see if it is the same, but that requires knowledge of how to compare strings (winbatch has indicated some of the ways to do that). An alternative is to realize that a C style string is a null terminated char array. A palindrome is a word (or group of words), think string, that is spelled the same forward as backward. That means the first and last letter, think char, of a palindrome are the same, the second and second to last letter are the same, etc. So what? Well, you can determine the length of a word by counting the letters. You can determine the length of a string by counting the char, or, if you are familiar with strlen(), you can use it to determine the length of a C style string. strlen() returns the number of non-null char in a C style string. Once you know the length of the string you can determine what int is the largest number that is less than one half the length of the string. Then use a loop to look at each char in the first half of the string in ascending order and compare it with each char in the second half of the string starting from the end of the string and working back toward the middle. If each comparison is a match, then the string is a palindrome. The first time you have a mismatch you know the word/phrase/string isn't a palindrome and you can break out of the comparison.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:18 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC