// #palindrome #C #Programming #String
#include <stdio.h>
#include <string.h>
typedef unsigned int uint;
uint find_next_palin(uint inp);
main()
{
uint num;
printf("enter the number: ");
scanf("%u", &num);
uint ret = find_next_palin(num);
printf("next palindrome is %u\n", ret);
}
uint find_next_palin(uint inp)
{
char buff[32];
uint ret, i1 =0, i2 =0, i = 0;
sprintf(buff, "%d", inp);
int len = strlen(buff);
if (len == 1) {
return inp;
}
if (len % 2 == 0) {
i1 = buff[len/2 -1] - '0';
i2 = buff[len/2] - '0';
if (i1 < i2) {
buff[len/2 -1] = i1 + '0'+1;
}
for (i = 0; i < len/2; i++) {
buff[len/2+i] = buff[len/2-i-1];
}
} else {
i1 = buff[len/2 -1] - '0';
i2 = buff[len/2 +1] - '0';
if (i1 < i2) {
buff[len/2 -1] = i1 + '0'+1;
}
for (i = 0; i < len/2; i++) {
buff[len/2+i+1] = buff[len/2-i-1];
}
}
sscanf(buff, "%u", &ret);
return ret;
}
#include <stdio.h>
#include <string.h>
typedef unsigned int uint;
uint find_next_palin(uint inp);
main()
{
uint num;
printf("enter the number: ");
scanf("%u", &num);
uint ret = find_next_palin(num);
printf("next palindrome is %u\n", ret);
}
uint find_next_palin(uint inp)
{
char buff[32];
uint ret, i1 =0, i2 =0, i = 0;
sprintf(buff, "%d", inp);
int len = strlen(buff);
if (len == 1) {
return inp;
}
if (len % 2 == 0) {
i1 = buff[len/2 -1] - '0';
i2 = buff[len/2] - '0';
if (i1 < i2) {
buff[len/2 -1] = i1 + '0'+1;
}
for (i = 0; i < len/2; i++) {
buff[len/2+i] = buff[len/2-i-1];
}
} else {
i1 = buff[len/2 -1] - '0';
i2 = buff[len/2 +1] - '0';
if (i1 < i2) {
buff[len/2 -1] = i1 + '0'+1;
}
for (i = 0; i < len/2; i++) {
buff[len/2+i+1] = buff[len/2-i-1];
}
}
sscanf(buff, "%u", &ret);
return ret;
}
No comments:
Post a Comment