Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Test if an integer is a palindrome in c language

Reply
Thread Tools

Test if an integer is a palindrome in c language

 
 
Wabz
Guest
Posts: n/a
 
      08-14-2007
Hello mates,

Does anyone know how to write a function that tests if an integer is a
palindrome in C language?

 
Reply With Quote
 
 
 
 
Kevin Handy
Guest
Posts: n/a
 
      08-14-2007
Wabz wrote:

> Does anyone know how to write a function that tests if an integer is a
> palindrome in C language?


Yes. I'm quite sure that someone does.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
 
Wabz
Guest
Posts: n/a
 
      08-14-2007
On Aug 14, 1:19 pm, Kevin Handy <(E-Mail Removed)> wrote:
> Wabz wrote:
> > Does anyone know how to write a function that tests if an integer is a
> >palindromein C language?

>
> Yes. I'm quite sure that someone does.
>
> ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----http://www.newsfeeds.comThe #1 Newsgroup Service in the World! 120,000+ Newsgroups
> ----= East and West-Coast Server Farms - Total Privacy via Encryption =----


Could this someone possibly help me write the said program?

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-14-2007
Wabz wrote, On 14/08/07 18:42:
> On Aug 14, 1:19 pm, Kevin Handy <(E-Mail Removed)> wrote:
>> Wabz wrote:
>>> Does anyone know how to write a function that tests if an integer is a
>>> palindromein C language?

>> Yes. I'm quite sure that someone does.
>>
>> ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----http://www.newsfeeds.comThe #1 Newsgroup Service in the World! 120,000+ Newsgroups
>> ----= East and West-Coast Server Farms - Total Privacy via Encryption =----


Please don't quote things you are not commenting on, such as the
annoying text appended to Kevin's post.

> Could this someone possibly help me write the said program?


Yes, I'm sure one of the people meeting those requirements could help if
they were so desired. However, knowledgeable people are unlikely to do
your homework for you when you have not even made an attempt. So if you
want help you should attempt it and then post your attempt here with any
questions you have. The more effort you put in the more people will be
prepared to help you.
--
Flash Gordon
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      08-14-2007
"Wabz" writes:

> On Aug 14, 1:19 pm, Kevin Handy <(E-Mail Removed)> wrote:
>> Wabz wrote:
>> > Does anyone know how to write a function that tests if an integer is a
>> >palindromein C language?

>>
>> Yes. I'm quite sure that someone does.


> Could this someone possibly help me write the said program?


Start by extracting the individual digits of the integer. You may find the
modulo operator, %, helpful. There is another way, involving sprintf() in
<stdio.h>, but if this is a school assignment, the instructor probably
expects the modulo way.


 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      08-14-2007

"Wabz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello mates,
>
> Does anyone know how to write a function that tests if an integer is a
> palindrome in C language?
>

What base? Eleven is a palindrome when written in base 10,
but not when written in base 8.
Create a buffer large enough to hold the characters to display the
largest integer. Use sprintf to fill it. Check whether palindrome.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing


 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      08-14-2007
On Tue, 14 Aug 2007 17:10:51 +0000, Wabz wrote:

> Hello mates,
>
> Does anyone know how to write a function that tests if an integer is a
> palindrome in C language?

I assume you mean palindrome in base 10.
sprintf() it to a string, and then check whether the string is
palindrome:
len = strlen(str);
for (i = 0; i < len/2; i++) {
if (str[i] != str[len - i - 1])
break; /*it is not palindome*/
}

--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

 
Reply With Quote
 
Kevin Handy
Guest
Posts: n/a
 
      08-14-2007
Wabz wrote:

> Could this someone possibly help me write the said program?


Should have asked that in the first place.

Many ways of doing it. You need to decide
what number system you want to
use: binary, hex, octal, decimal, etc.

1. Convert to string in that format, and compare
front end of the resulting string to the back end.

2. A number can be a palindrome in one base, while
not being one in another. If you can choose your
base for a specific number N, you can pick
base (N-1), which gives you the result (11),
which is always a palindrome. So your program
could then be hard coded to always print "true".

3. Shift digits of N into a new value, in reverse
order (% helps here). Compare N and reverse N.

....

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      08-14-2007
On Aug 14, 11:20 am, Army1987 <(E-Mail Removed)> wrote:
> On Tue, 14 Aug 2007 17:10:51 +0000, Wabz wrote:
> > Hello mates,

>
> > Does anyone know how to write a function that tests if an integer is a
> > palindrome in C language?

>
> I assume you mean palindrome in base 10.


Otherwise (for instance) FFFF is a palindrome, but 65535 will say
'nope'

> sprintf() it to a string, and then check whether the string is
> palindrome:
> len = strlen(str);
> for (i = 0; i < len/2; i++) {
> if (str[i] != str[len - i - 1])
> break; /*it is not palindome*/
>
> }


It might be interesting to try every base from 2 to 36.
Given that condition, what percentage of integers are palindromes?

 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      08-14-2007
On Aug 14, 2:15 pm, user923005 <(E-Mail Removed)> wrote:
> On Aug 14, 11:20 am, Army1987 <(E-Mail Removed)> wrote:
>
> > On Tue, 14 Aug 2007 17:10:51 +0000, Wabz wrote:
> > > Hello mates,

>
> > > Does anyone know how to write a function that tests if an integer is a
> > > palindrome in C language?

>
> > I assume you mean palindrome in base 10.

>
> Otherwise (for instance) FFFF is a palindrome, but 65535 will say
> 'nope'
>
> > sprintf() it to a string, and then check whether the string is
> > palindrome:
> > len = strlen(str);
> > for (i = 0; i < len/2; i++) {
> > if (str[i] != str[len - i - 1])
> > break; /*it is not palindome*/

>
> > }

>
> It might be interesting to try every base from 2 to 36.
> Given that condition, what percentage of integers are palindromes?


It seems that what numbers are not palindromes in some base might be a
more interesting question. Of course if we include bases up to that
number, then the answer is obviously 'none'.

#include <stdlib.h>
#include <string.h>
/* ltostr is from snippets (I fixed the broken bit). */
char *ltostr(long long num, char *string, size_t max_chars, unsigned
base)
{
char remainder;
int sign = 0;
if (base < 2 || base > 36)
return ((void *) 0);
if (num < 0) {
sign = 1;
num = -num;
}
if (num == 0) /* bugbug:drc formerly wrong result here... */
return "0";
string[--max_chars] = '\0';
for (max_chars--; max_chars > sign && num != 0; max_chars--) {
remainder = (char) (num % base);
if (remainder < 9)
string[max_chars] = remainder + '0';
else
string[max_chars] = remainder - 10 + 'A';
num /= base;
}
if (sign)
string[--max_chars] = '-';
if (max_chars > 0)
memset(string, ' ', max_chars + 1);
return string + max_chars;
}

int ispal(const char *start)
{
const char *end = start + strlen(start) - 1;
while (end > start) {
if (*start != *end) {
return 0;
}
start++;
end--;
}
return 1;
}

#include <stdio.h>
static char string[50] = {0};
int main(void)
{
long long index;
unsigned base;
for (index = 0; index < 4000000000; index++) {
for (base = 2; base < 37; base++) {
long long val = index;
char *p = ltostr(val, string, sizeof string,
base);
if (isspace(*p))
p++;
if (ispal(p)) {
printf("%llu = %s is a palindrome in base %u\n", val,
p, base);
}
}
}
return 0;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Re: Palindrome Tim Churches Python 2 06-05-2011 03:24 AM
Palindrome Runic911 Python 24 11-15-2003 12:08 AM
Re: Palindrome Pierre Quentel Python 2 11-13-2003 06:11 PM
Palindrome (HELP) Lorin Leone C++ 4 11-13-2003 08:11 AM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments