-
Notifications
You must be signed in to change notification settings - Fork 0
/
1317.c
63 lines (52 loc) · 855 Bytes
/
1317.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
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#define N 50
int read_line(char *str, int n);
bool is_palindrome(const char *message);
int
main (int argc, char *argv[])
{
char message[N+1];
printf("Enter a message: ");
read_line(message, N+1);
if (is_palindrome(message))
{
printf("Palindrome.\n");
}
else
{
printf("Not a palidrome.\n");
}
return 0;
}
int
read_line(char *message, int n)
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
{
if ((ch > 90 && ch < 97) || ch < 65 || ch > 122)
{
continue;
}
message[i++] = toupper(ch);
}
message[i] = '\0';
return i;
}
bool
is_palindrome(const char *message)
{
int i = strlen(message), j;
bool flag = true;
for (j = i - 1; j >= 0; j--)
{
if (message[j] != message[i - j - 1])
{
flag = false;
}
}
return flag;
}