473,549 Members | 5,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Aug 14 '07 #1
20 13369
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 =----
Aug 14 '07 #2
On Aug 14, 1:19 pm, Kevin Handy <k...@srv.netwr ote:
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.c omThe #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?

Aug 14 '07 #3
"Wabz" writes:
On Aug 14, 1:19 pm, Kevin Handy <k...@srv.netwr ote:
>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.
Aug 14 '07 #4
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

Aug 14 '07 #5

"Wabz" <Wa*****@gmail. comwrote in message
news:11******** **************@ b79g2000hse.goo glegroups.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
Aug 14 '07 #6
Wabz wrote, On 14/08/07 18:42:
On Aug 14, 1:19 pm, Kevin Handy <k...@srv.netwr ote:
>Wabz wrote:
>>Does anyone know how to write a function that tests if an integer is a
palindromei n C language?
Yes. I'm quite sure that someone does.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----http://www.newsfeeds.c omThe #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
Aug 14 '07 #7
On Aug 14, 11:20 am, Army1987 <army1...@NOSPA M.itwrote:
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?

Aug 14 '07 #8
On Aug 14, 2:15 pm, user923005 <dcor...@connx. comwrote:
On Aug 14, 11:20 am, Army1987 <army1...@NOSPA M.itwrote:
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;
}

Aug 14 '07 #9
/* Original ltostr() was hosed. Fixed version: */
static const char B36TAB[] =
{
'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
#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);
string[max_chars] = B36TAB[remainder];
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;
}
/*
Sample output:
....
2290134 = E656E is a palindrome in base 20
2290141 = 84548 is a palindrome in base 23
2290156 = 1IEI1 is a palindrome in base 35
2290187 = 9H1H9 is a palindrome in base 22
2290220 = 36Q63 is a palindrome in base 29
2290252 = 11022100122011 is a palindrome in base 3
2290293 = 50805 is a palindrome in base 26
2290322 = 2OOO2 is a palindrome in base 30
2290353 = 925529 is a palindrome in base 12
2290378 = 4270724 is a palindrome in base 9
2290391 = HAHAH is a palindrome in base 19
2290460 = BG6GB is a palindrome in base 21
2290499 = 2ERE2 is a palindrome in base 31
2290501 = 1D3D1 is a palindrome in base 36
2290530 = 5LEL5 is a palindrome in base 25
2290534 = E666E is a palindrome in base 20
2290627 = 3K9K3 is a palindrome in base 28
2290641 = 100010111100111 1010001 is a palindrome in base 2
2290658 = 20233033202 is a palindrome in base 4
2290670 = 84648 is a palindrome in base 23
2290671 = 9H2H9 is a palindrome in base 22
2290686 = 6LGL6 is a palindrome in base 24
2290735 = 13EE31 is a palindrome in base 18
2290738 = 48A84 is a palindrome in base 27
2290752 = HAIAH is a palindrome in base 19
2290850 = 25T52 is a palindrome in base 32
2290853 = 1O9O1 is a palindrome in base 34
2290901 = BG7GB is a palindrome in base 21
2290922 = 2290922 is a palindrome in base 10
....
*/
Aug 14 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
14050
by: NotGiven | last post by:
I need to check the $_GET to make certain it is a positive integer. is_integer($_GET) is not working. I think it thinks it's a sting. So I tried casting it to an int using, is_integer((int)$_GET). Then whatever I threw in the _GET variable passed as an integer, even strings. Any ideas or links or functions are much appreciated!
24
12989
by: Runic911 | last post by:
Does anyone know how i can fix my Palindrome program? s = raw_input('Enter a String: ') punctuation = '%$!*.,-:? ;()\'\"\\' i = 0 h = 0 t = 0 p = '' z = 0 while s!= ' ':
3
1685
by: Ken Fine | last post by:
I periodically receive a 5+ MB XML document that I hand-load into SQL Server using SQLXML running under a DTS process. Unfortunately, the document is human-created, and (very unfortunately) often has invalid elements, which breaks the bulk load. I've been managing this problem by loading the document into Visual Studio and using it to...
4
13847
by: Lorin Leone | last post by:
Can anyone help me modify the program so that it recognizes strings like "Anna" as palindromes. To make the program "case-insensitive." using the built-in C++ function "toupper". and so that it recognizes strings like "race car" as palindromes, have the program ignore spaces in the input string. Here is my code:
1
1634
by: SAN CAZIANO | last post by:
is there a function that get the name of the first input field of the current form ? in my example below I want create an array of form field name and in the onsubmit assign all element's name to create a simple iteration to test if some elements in my array, that must be required, are null: something like function verify(array of string) ...
2
1466
by: Stanley Sinclair | last post by:
Has the promise of stored procedures written in VB6 been fulfilled in 8.2? If there special syntax, and where do I find it, and where are samples? Would it be as efficient as, say, SQL?
6
1973
by: trentdk | last post by:
I want to test which language (testing C and FORTRAN) would be faster with math calculations; one test with intergers, and another test with floats. What math formulas/functions would you guys use that are simple, yet will tax the processor and the abilities of the languages? Thanks =)
1
2819
by: Nigel Findlater | last post by:
I have been using Visual Studio 2003 to generate aspx and aspx.vb file. On my development platform they all work fine, but when I load them on the server I get the message: Parser Error Message: Could not load type 'test'. Source Error: Line 1: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="test.aspx.vb" Inherits="test"%>
6
1737
by: deanhiller | last post by:
In C++ or Java, or whatever OO, I am used to using the Dependency Injection Pattern(or I believe formerly called Inversion of Control Pattern) to accomplish test first in a large system. ie. I would have 5 architecture components, and I would simply create a mock object of one of those systems like Station s =...
0
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7962
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7814
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.