Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Test if an integer is a palindrome in c language (http://www.velocityreviews.com/forums/t529183-test-if-an-integer-is-a-palindrome-in-c-language.html)

Wabz 08-14-2007 05:10 PM

Test if an integer is a palindrome in c language
 
Hello mates,

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


Kevin Handy 08-14-2007 05:19 PM

Re: Test if an integer is a palindrome in c language
 
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 =----

Wabz 08-14-2007 05:42 PM

Re: Test if an integer is a palindrome in c language
 
On Aug 14, 1:19 pm, Kevin Handy <k...@srv.net> 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?


Flash Gordon 08-14-2007 06:06 PM

Re: Test if an integer is a palindrome in c language
 
Wabz wrote, On 14/08/07 18:42:
> On Aug 14, 1:19 pm, Kevin Handy <k...@srv.net> 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

osmium 08-14-2007 06:10 PM

Re: Test if an integer is a palindrome in c language
 
"Wabz" writes:

> On Aug 14, 1:19 pm, Kevin Handy <k...@srv.net> 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.



Fred Kleinschmidt 08-14-2007 06:20 PM

Re: Test if an integer is a palindrome in c language
 

"Wabz" <Wakauma@gmail.com> wrote in message
news:1187111451.825550.255970@b79g2000hse.googlegr 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



Army1987 08-14-2007 06:20 PM

Re: Test if an integer is a palindrome in c language
 
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


Kevin Handy 08-14-2007 06:31 PM

Re: Test if an integer is a palindrome in c language
 
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 =----

user923005 08-14-2007 09:15 PM

Re: Test if an integer is a palindrome in c language
 
On Aug 14, 11:20 am, Army1987 <army1...@NOSPAM.it> 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?


user923005 08-14-2007 10:02 PM

Re: Test if an integer is a palindrome in c language
 
On Aug 14, 2:15 pm, user923005 <dcor...@connx.com> wrote:
> On Aug 14, 11:20 am, Army1987 <army1...@NOSPAM.it> 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;
}



All times are GMT. The time now is 11:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57