-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.c
More file actions
66 lines (53 loc) · 1.23 KB
/
palindrome.c
File metadata and controls
66 lines (53 loc) · 1.23 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
58
59
60
61
62
63
64
65
66
#include <stdlib.h>
#include <stdio.h>
#define MAX_LEN 10
int main(void)
{
char c=EOF;
unsigned int len_max=128;
unsigned int i=0;
unsigned int current_size;
current_size=len_max;
char *ptr=malloc(len_max);
/*
Changing the pointer malloc'd will produce undefined behavior for free()
If pointer incrementing were to be used after a malloc, the original starting
address must be maintained and used at the argument to free().
*/
if(ptr!=NULL)
{
while((c=getchar())!='\n'&& c!=EOF)
{
if(isalpha(c))
ptr[i++]=(char)c;
if(i>=current_size)
{
current_size=i+len_max;
ptr=realloc(ptr, current_size);
}
}
ptr[i]='\0';
printf("%s\n", ptr);
if(isPalindrome(ptr, i))
printf("is a palindrome\n");
else
printf("is not a palindrome\n");
free(ptr);
ptr=NULL;
}
return 0;
}
int isPalindrome(char *ptr, int i)
{
int x = 0;
int mid_point = i/2;
char *ptr2=ptr; //ptr2 is at the end of the array//
while(i>=mid_point)
if(ptr[x++]!=ptr2[--i])
{
ptr2=NULL;
return 0;
}
ptr2=NULL;
return 1;
}