Skip to content

Commit 05955f5

Browse files
committed
My project on Recursion
1 parent 31c7388 commit 05955f5

11 files changed

+358
-0
lines changed

0x08-recursion/0-puts_recursion.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "main.h"
2+
3+
/**
4+
* _puts_recursion - Prints a string, followed by a new line.
5+
* @s: The string.
6+
*/
7+
void _puts_recursion(char *s)
8+
{
9+
if (*s)
10+
{
11+
_putchar(*s);
12+
_puts_recursion(s + 1);
13+
}
14+
15+
else
16+
_putchar('\n');
17+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "main.h"
2+
3+
/**
4+
* _print_rev_recursion - Prints a string in reverse.
5+
* @s: The string to be printed.
6+
*/
7+
void _print_rev_recursion(char *s)
8+
{
9+
if (*s)
10+
{
11+
_print_rev_recursion(s + 1);
12+
_putchar(*s);
13+
}
14+
}

0x08-recursion/100-is_palindrome.c

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "main.h"
2+
3+
int find_strlen(char *s);
4+
int check_palindrome(char *s, int len, int index);
5+
int is_palindrome(char *s);
6+
7+
/**
8+
* find_strlen - Returns the length of a string.
9+
* @s: The string to be measured.
10+
*
11+
* Return: The length of the string.
12+
*/
13+
int find_strlen(char *s)
14+
{
15+
int len = 0;
16+
17+
if (*(s + len))
18+
{
19+
len++;
20+
len += find_strlen(s + len);
21+
}
22+
23+
return (len);
24+
}
25+
26+
/**
27+
* check_palindrome - Checks if a string is a palindrome.
28+
* @s: The string to be checked.
29+
* @len: The length of s.
30+
* @index: The index of the string to be checked.
31+
*
32+
* Return: If the string is a palindrome - 1.
33+
* If the string is not a palindrome - 0.
34+
*/
35+
int check_palindrome(char *s, int len, int index)
36+
{
37+
if (s[index] == s[len / 2])
38+
return (1);
39+
40+
if (s[index] == s[len - index - 1])
41+
return (check_palindrome(s, len, index + 1));
42+
43+
return (0);
44+
}
45+
46+
/**
47+
* is_palindrome - Checks if a string is a palindrome.
48+
* @s: The string to be checked.
49+
*
50+
* Return: If the string is a palindrome - 1.
51+
* If the string is not a palindrome - 0.
52+
*/
53+
int is_palindrome(char *s)
54+
{
55+
int index = 0;
56+
int len = find_strlen(s);
57+
58+
if (!(*s))
59+
return (1);
60+
61+
return (check_palindrome(s, len, index));
62+
}

0x08-recursion/101-wildcmp.c

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "main.h"
2+
3+
int strlen_no_wilds(char *str);
4+
void iterate_wild(char **wildstr);
5+
char *postfix_match(char *str, char *postfix);
6+
int wildcmp(char *s1, char *s2);
7+
8+
/**
9+
* strlen_no_wilds - Returns the length of a string,
10+
* ignoring wildcard characters.
11+
* @str: The string to be measured.
12+
*
13+
* Return: The length.
14+
*/
15+
int strlen_no_wilds(char *str)
16+
{
17+
int len = 0, index = 0;
18+
19+
if (*(str + index))
20+
{
21+
if (*str != '*')
22+
len++;
23+
24+
index++;
25+
len += strlen_no_wilds(str + index);
26+
}
27+
28+
return (len);
29+
}
30+
31+
/**
32+
* iterate_wild - Iterates through a string located at a wildcard
33+
* until it points to a non-wildcard character.
34+
* @wildstr: The string to be iterated through.
35+
*/
36+
void iterate_wild(char **wildstr)
37+
{
38+
if (**wildstr == '*')
39+
{
40+
(*wildstr)++;
41+
iterate_wild(wildstr);
42+
}
43+
}
44+
45+
/**
46+
* postfix_match - Checks if a string str matches the postfix of
47+
* another string potentially containing wildcards.
48+
* @str: The string to be matched.
49+
* @postfix: The postfix.
50+
*
51+
* Return: If str and postfix are identical - a pointer to the null byte
52+
* located at the end of postfix.
53+
* Otherwise - a pointer to the first unmatched character in postfix.
54+
*/
55+
char *postfix_match(char *str, char *postfix)
56+
{
57+
int str_len = strlen_no_wilds(str) - 1;
58+
int postfix_len = strlen_no_wilds(postfix) - 1;
59+
60+
if (*postfix == '*')
61+
iterate_wild(&postfix);
62+
63+
if (*(str + str_len - postfix_len) == *postfix && *postfix != '\0')
64+
{
65+
postfix++;
66+
return (postfix_match(str, postfix));
67+
}
68+
69+
return (postfix);
70+
}
71+
72+
/**
73+
* wildcmp - Compares two strings, considering wildcard characters.
74+
* @s1: The first string to be compared.
75+
* @s2: The second string to be compared - may contain wildcards.
76+
*
77+
* Return: If the strings can be considered identical - 1.
78+
* Otherwise - 0.
79+
*/
80+
int wildcmp(char *s1, char *s2)
81+
{
82+
if (*s2 == '*')
83+
{
84+
iterate_wild(&s2);
85+
s2 = postfix_match(s1, s2);
86+
}
87+
88+
if (*s2 == '\0')
89+
return (1);
90+
91+
if (*s1 != *s2)
92+
return (0);
93+
94+
return (wildcmp(++s1, ++s2));
95+
}

0x08-recursion/2-strlen_recursion.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "main.h"
2+
3+
/**
4+
* _strlen_recursion - Returns the length of a string.
5+
* @s: The string to be measured.
6+
*
7+
* Return: The length of the string.
8+
*/
9+
int _strlen_recursion(char *s)
10+
{
11+
int len = 0;
12+
13+
if (*s)
14+
{
15+
len++;
16+
len += _strlen_recursion(s + 1);
17+
}
18+
19+
return (len);
20+
}

0x08-recursion/3-factorial.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "main.h"
2+
3+
/**
4+
* factorial - Returns the factorial of a given number.
5+
* @n: The number to find the factorial of.
6+
*
7+
* Return: If n > 0 - the factorial of n.
8+
* If n < 0 - 1 to indicate an error.
9+
*/
10+
int factorial(int n)
11+
{
12+
int result = n;
13+
14+
if (n < 0)
15+
return (-1);
16+
17+
else if (n >= 0 && n <= 1)
18+
return (1);
19+
20+
result *= factorial(n - 1);
21+
22+
return (result);
23+
}

0x08-recursion/4-pow_recursion.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "main.h"
2+
3+
/**
4+
* _pow_recursion - Returns the value of x raised to the power of y.
5+
* @x: The number to be raised.
6+
* @y: The power.
7+
*
8+
* Return: The value of x raised to the power of y.
9+
*/
10+
int _pow_recursion(int x, int y)
11+
{
12+
int result = x;
13+
14+
if (y < 0)
15+
return (-1);
16+
17+
else if (y == 0)
18+
return (1);
19+
20+
result *= _pow_recursion(x, y - 1);
21+
22+
return (result);
23+
}

0x08-recursion/5-sqrt_recursion.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "main.h"
2+
3+
int find_sqrt(int num, int root);
4+
int _sqrt_recursion(int n);
5+
6+
/**
7+
* find_sqrt - Finds the natural square root of an inputted number.
8+
* @num: The number to find the square root of.
9+
* @root: The root to be tested.
10+
*
11+
* Return: If the number has a natural square root - the square root.
12+
* If the number does not have a natural square root - -1.
13+
*/
14+
int find_sqrt(int num, int root)
15+
{
16+
if ((root * root) == num)
17+
return (root);
18+
19+
if (root == num / 2)
20+
return (-1);
21+
22+
return (find_sqrt(num, root + 1));
23+
}
24+
25+
/**
26+
* _sqrt_recursion - Returns the natural square root of a number.
27+
* @n: The number to return the square root of.
28+
*
29+
* Return: If n has a natural square root - the natural square root of n.
30+
* If n does not have a natural square root - -1.
31+
*/
32+
int _sqrt_recursion(int n)
33+
{
34+
int root = 0;
35+
36+
if (n < 0)
37+
return (-1);
38+
39+
if (n == 1)
40+
return (1);
41+
42+
return (find_sqrt(n, root));
43+
}

0x08-recursion/6-is_prime_number.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "main.h"
2+
3+
int is_divisible(int num, int div);
4+
int is_prime_number(int n);
5+
6+
/**
7+
* is_divisible - Checks if a number is divisible.
8+
* @num: The number to be checked.
9+
* @div: The divisor.
10+
*
11+
* Return: If the number is divisible - 0.
12+
* If the number is not divisible - 1.
13+
*/
14+
int is_divisible(int num, int div)
15+
{
16+
if (num % div == 0)
17+
return (0);
18+
19+
if (div == num / 2)
20+
return (1);
21+
22+
return (is_divisible(num, div + 1));
23+
}
24+
25+
/**
26+
* is_prime_number - Checks if a number is prime.
27+
* @n: The number to be checked.
28+
*
29+
* Return: If the integer is not prime - 0.
30+
* If the number is prime - 1.
31+
*/
32+
int is_prime_number(int n)
33+
{
34+
int div = 2;
35+
36+
if (n <= 1)
37+
return (0);
38+
39+
if (n >= 2 && n <= 3)
40+
return (1);
41+
42+
return (is_divisible(n, div));
43+
}

0x08-recursion/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0x08. C - Recursion
2+
3+
The project that introduced me to recursion under the auspieces of ALX while I was training as a Software Engineer.

0x08-recursion/main.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef main_h
2+
#define main_h
3+
4+
int _putchar(char c);
5+
void _puts_recursion(char *s);
6+
void _print_rev_recursion(char *s);
7+
int _strlen_recursion(char *s);
8+
int factorial(int n);
9+
int _pow_recursion(int x, int y);
10+
int _sqrt_recursion(int n);
11+
int is_prime_number(int n);
12+
int wildcmp(char *s1, char *s2);
13+
int is_palindrome(char *s);
14+
15+
#endif

0 commit comments

Comments
 (0)