-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-is_palindrome.c
62 lines (52 loc) · 1.16 KB
/
100-is_palindrome.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
#include "main.h"
int find_strlen(char *s);
int check_palindrome(char *s, int len, int index);
int is_palindrome(char *s);
/**
* find_strlen - Returns the length of a string.
* @s: The string to be measured.
*
* Return: The length of the string.
*/
int find_strlen(char *s)
{
int len = 0;
if (*(s + len))
{
len++;
len += find_strlen(s + len);
}
return (len);
}
/**
* check_palindrome - Checks if a string is a palindrome.
* @s: The string to be checked.
* @len: The length of s.
* @index: The index of the string to be checked.
*
* Return: If the string is a palindrome - 1.
* If the string is not a palindrome - 0.
*/
int check_palindrome(char *s, int len, int index)
{
if (s[index] == s[len / 2])
return (1);
if (s[index] == s[len - index - 1])
return (check_palindrome(s, len, index + 1));
return (0);
}
/**
* is_palindrome - Checks if a string is a palindrome.
* @s: The string to be checked.
*
* Return: If the string is a palindrome - 1.
* If the string is not a palindrome - 0.
*/
int is_palindrome(char *s)
{
int index = 0;
int len = find_strlen(s);
if (!(*s))
return (1);
return (check_palindrome(s, len, index));
}