c - Finding next smallest palindrome, infinite loop -


i writing code find next smallest palindrome(integer) . (must) using array deal large numbers below:

#include<stdio.h> #include<malloc.h> #include<string.h> void check_pal(int a[],int max) {     int i,j,ctr,k;     while(1)     {     for(i=0;i<max;i++)     printf("%d",a[i]);          ctr=0;          k=max-1;     while(a[k]==9)     {         a[k--]=0;//add corner case when k==0      }     a[k]++;     for(i=0,j=max;i<max/2;i++,j--)        {            printf("%d",i);         if(a[i]!=a[j])             {                 ctr=1;                 break;             }        }     if(ctr==0)     for(i=0;i<max;i++)     {          printf("%d",a[i]);          if(i==max-1)             return;     }     } } void int_convert(char * m,int a[] ) {      int i,max;     for(i=0;i<strlen(m);i++)     {        //  printf("%c",m[i]);         a[i]=m[i]-'0';     }      max=strlen( m);     printf("%d\n",max);     check_pal(a,max);  } void main() {   int a[200],max;     char * m=malloc(sizeof(char)*200);     scanf("%s",m);     int_convert(m,a);     getch(); } 

the output result infinite loop . e.g. input 45 output must 55 resulting in 0000000 .. please tell me wrong .

it not difficult recognize palindromes:

int is_palindrome(int a[], int max) {     (int = 0; < max/2; i++) {         if (a[i] != a[max-i-1]) {             return 0;         }     }     return 1; } 

it not difficult increment value:

void next_value(int a[], int max) {     int = max - 1;     a[i]++;     while (i > 0 && a[i] > 9) {         a[i] = 0;         a[i-1]++;         i--;     } } 

it's easy display value:

void show(int a[], int max) {     (int = 0; < max; i++) {         printf("%d", a[i]);     }     printf("\n"); } 

with support it's trivial find smallest following palindrome:

void check_pal(int a[], int max) {     while (!is_palindrome(a, max)) {         next_value(a, max);     }     show(a, max); } 

by way, call function find_pal rather check_pal.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -