-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path004.c
57 lines (44 loc) · 1018 Bytes
/
004.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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_palindrome(char* string);
int main(int argc, char **argv)
{
char buffer[7];
int i, j, product;
int max_palindrome = 0;
for (i = 100; i < 1000; i++)
{
for (j = 100; j < 1000; j++)
{
product = i * j;
sprintf(buffer, "%i", product);
if (is_palindrome(buffer) && product > max_palindrome)
{
max_palindrome = product;
}
}
}
printf("The largest palindrome is %i.\n", max_palindrome);
exit(EXIT_SUCCESS);
}
int is_palindrome(char* string)
{
int same_forward_and_backward = 0;
size_t length = strlen(string);
char *reverse = malloc(length + 1);
size_t i = 0;
while (string[i] != '\0')
{
reverse[length-1-i] = string[i];
i++;
}
reverse[length] = '\0';
if (0 == strcmp(string, reverse))
{
same_forward_and_backward = 1;
}
free(reverse);
return same_forward_and_backward;
}