-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_palindrome.c
More file actions
57 lines (47 loc) · 1.5 KB
/
max_palindrome.c
File metadata and controls
57 lines (47 loc) · 1.5 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int palin(char [], int, int);
void copy(char [], const char dummy[], int, int);
int main ()
{
char *text = (char *) calloc(1024, sizeof(char));
printf ("\nEnter any text: ");
fgets(text, 1023, stdin);
*(text + strlen(text) - 1) = '\0';
int len = strlen(text);
char curr_max[len];
int count, count1, curr_max_len = 0, max_len = 0, max_strt = 0, max_end = 0;
for (count = 0; count < len; count++){
for (count1 = len - 1; count1 >= count; count1--){
if(palin(text, count, count1)){
max_len = count1 - count;
if(curr_max_len == 0 || curr_max_len < max_len){
curr_max_len = max_len;
max_strt = count;
max_end = count1;
}
}
}
}
copy(curr_max, text, max_strt, max_end);
printf (" %s", curr_max);
return 0;
}
int palin(char test[], int strt, int end){
unsigned count, count1, loop = (strt + end)/2;
for(count = strt, count1 = end; count <= loop; count++, count1--){
if(test[count] != test[count1]){
return 0;
}
}
return 1;
}
void copy(char dest[], const char src[], int strt, int end){
unsigned count;
for ( count = 0; count + strt <= end; count++){
dest[count] = src[count + strt];
}
dest[count] = '\0';
return ;
}