-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitwiseandofnumbersinrange.c
68 lines (62 loc) · 1.4 KB
/
bitwiseandofnumbersinrange.c
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
58
59
60
61
62
63
64
65
66
67
68
#include<stdlib.h>
#include<string.h>
#include <stdio.h>
int isPalindrome(int x){
char stringform[32];
//itoa(x,stringform,10);
/* if x is negatinve return 0 */
if (x < 0)
{
return 0;
}
/* if only 1 digit return true */
if (0 == x/10)
{
return 1;
}
int j = 0;
/* keep dividing until we get all the numbers */
int number = x;
do
{
stringform[j] = (number % 10) + 0x30;
number = number/10;
//printf("%c\n", stringform[j]);
j++;
}while(number > 0);
/* this is in reverse form */
stringform[j] = '\0';
printf("strignform = %s\n", stringform);
/* reverse the string again */
char reverse[32];
char *start = stringform;
while(*start != '\0')
{
start++;
}
start--;
/* this is the last number in char form */
printf("0x%x, 0x%x", stringform, start);
int i = 0;
while(stringform <= start)
{
//printf("here1");
reverse[i] = *start;
printf("%c\n", reverse[i]);
i++;
start--;
}
//i++;
reverse[i]='\0';
printf("\nreverse = %s", reverse);
/*check again*/
if (0 == strcmp(stringform, reverse))
{
return 1;
}
return 0;
}
void main()
{
printf("\nres = %d\n",isPalindrome(1000000001));
}