Skip to content

Commit 01cdb5b

Browse files
committed
Add recursive programs
1 parent e17cea0 commit 01cdb5b

7 files changed

+281
-0
lines changed

Recursion/char_replace.c

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <string.h>
2+
#include <assert.h>
3+
4+
/**
5+
* Replace char ch with given character in a string.
6+
*
7+
* @param char* str
8+
* @param char ch Character to replace
9+
* @param char with Replacing character
10+
* @param int index Current index
11+
*
12+
* return void
13+
*/
14+
void char_replace(char *str, char ch, char with, int index)
15+
{
16+
if (str[index] == '\0') {
17+
return;
18+
}
19+
20+
if (str[index] == ch) {
21+
str[index] = with;
22+
}
23+
24+
char_replace(str, ch, with, index + 1);
25+
}
26+
27+
int main()
28+
{
29+
char test_str[7][120] = {
30+
"ankit", "ankit pokhrel", "namaste", "This is a crazy world!", "C programming language",
31+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
32+
"Pellentesque varius, lorem id consequat elementum, elit dolor suscipit neque, vel rhoncus turpis est vitae lacus."
33+
};
34+
35+
char test_replace[7][2] = {
36+
{'n', 'e'}, {'k', 'x'}, {'a', 'p'}, {'i', 'q'},
37+
{'m', 'z'}, {'m', 'y'}, {'e', 'x'}
38+
};
39+
40+
char expected_output[7][120] = {
41+
"aekit", "anxit poxhrel", "npmpste", "Thqs qs a crazy world!", "C prograzzing language",
42+
"Lorey ipsuy dolor sit ayet, consectetur adipiscing elit.",
43+
"Pxllxntxsqux varius, lorxm id consxquat xlxmxntum, xlit dolor suscipit nxqux, vxl rhoncus turpis xst vitax lacus."
44+
};
45+
46+
for (int i = 0; i < 7; i++) {
47+
char_replace(test_str[i], test_replace[i][0], test_replace[i][1], 0);
48+
49+
assert(strcmp(expected_output[i], test_str[i]) == 0);
50+
}
51+
52+
return 0;
53+
}

Recursion/factorial.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <assert.h>
2+
3+
long long factorial(int n)
4+
{
5+
if (n < 2) {
6+
return n;
7+
}
8+
9+
return n * factorial(n - 1);
10+
}
11+
12+
int main()
13+
{
14+
assert(2 == factorial(2));
15+
assert(6 == factorial(3));
16+
assert(120 == factorial(5));
17+
assert(3628800 == factorial(10));
18+
assert(1307674368000 == factorial(15));
19+
assert(2432902008176640000 == factorial(20));
20+
21+
return 0;
22+
}

Recursion/nth_fibonacci_number.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <assert.h>
2+
3+
long int nth_fibonacci_number(unsigned int n)
4+
{
5+
if (n == 0) {
6+
return 0;
7+
}
8+
9+
if (n <= 2) {
10+
return 1;
11+
}
12+
13+
return nth_fibonacci_number(n - 1) + nth_fibonacci_number(n - 2);
14+
}
15+
16+
int main()
17+
{
18+
int fibonacci_numbers[13] = {
19+
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 233, 75025, 102334155
20+
};
21+
22+
unsigned int fibonacci_number_positions[13] = {
23+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 25, 40
24+
};
25+
26+
for (int i = 0; i < 13; i++) {
27+
assert(fibonacci_numbers[i] == nth_fibonacci_number(fibonacci_number_positions[i]));
28+
}
29+
30+
return 0;
31+
}

Recursion/palindrome.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdbool.h>
2+
#include <assert.h>
3+
4+
bool is_palindrome(char *str, int index, int size)
5+
{
6+
if (index > size / 2) {
7+
return true;
8+
}
9+
10+
if (str[index] != str[size - index]) {
11+
return false;
12+
}
13+
14+
return is_palindrome(str, index + 1, size);
15+
}
16+
17+
int main()
18+
{
19+
assert(!is_palindrome("ankit", 0, 4));
20+
assert(!is_palindrome("lirxil", 0, 5));
21+
assert(!is_palindrome("kaayak", 0, 5));
22+
assert(!is_palindrome("ka ya k", 0, 6));
23+
assert(!is_palindrome("namasman", 0, 7));
24+
25+
assert(is_palindrome("liril", 0, 4));
26+
assert(is_palindrome("kayak", 0, 4));
27+
assert(is_palindrome("racecar", 0, 6));
28+
assert(is_palindrome("rotator", 0, 6));
29+
assert(is_palindrome("rottaattor", 0, 9));
30+
31+
return 0;
32+
}

Recursion/str_reverse.c

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <assert.h>
5+
6+
char *substr(char *, size_t);
7+
8+
/**
9+
* Solution 1: By swapping elements.
10+
* Will override given string.
11+
*
12+
* @param char* str
13+
* @param int index
14+
* @param int size
15+
*
16+
* @return void
17+
*/
18+
void str_reverse_1(char *str, int index, int size)
19+
{
20+
if (index == size / 2) {
21+
return;
22+
}
23+
24+
char temp = str[index];
25+
str[index] = str[size - index];
26+
str[size - index] = temp;
27+
28+
str_reverse_1(str, index + 1, size);
29+
}
30+
31+
/**
32+
* Solution 2: By using temporary var.
33+
*
34+
* @param char* str
35+
*
36+
* @return char* reversed string
37+
*/
38+
char *str_reverse(char *str)
39+
{
40+
size_t len = strlen(str);
41+
42+
static char *reverse = "";
43+
static int i = 0;
44+
45+
if (i == 0) {
46+
reverse = (char *) malloc(len * sizeof(char));
47+
}
48+
49+
if (len < 1) {
50+
reverse[i] = '\0';
51+
i = 0;
52+
53+
return reverse;
54+
}
55+
56+
reverse[i++] = str[len - 1];
57+
58+
return str_reverse(substr(str, len));
59+
}
60+
61+
/**
62+
* Get substring of given length.
63+
*
64+
* @param char* str
65+
* @param size_t len
66+
*
67+
* @return char*
68+
*/
69+
char *substr(char *str, size_t len)
70+
{
71+
char *temp = (char *) malloc(len * sizeof(char));
72+
73+
strncpy(temp, str, len - 1);
74+
temp[len - 1] = '\0';
75+
76+
return temp;
77+
}
78+
79+
int main()
80+
{
81+
assert(strcmp("a", str_reverse("a")) == 0);
82+
assert(strcmp("tikna", str_reverse("ankit")) == 0);
83+
assert(strcmp("lerhkop tikna", str_reverse("ankit pokhrel")) == 0);
84+
assert(strcmp(".egaugnaL gnimmargorP C", str_reverse("C Programming Language.")) == 0);
85+
assert(strcmp("!dlrow yzarc a si sihT", str_reverse("This is a crazy world!")) == 0);
86+
assert(strcmp(
87+
".tile gnicsipida rutetcesnoc ,tema tis rolod muspi meroL",
88+
str_reverse("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
89+
) == 0);
90+
91+
char s[20] = "ankit";
92+
str_reverse_1(s, 0, (int) strlen(s) - 1);
93+
assert(strcmp("tikna", s) == 0);
94+
95+
strcpy(s, "ankit pokhrel");
96+
str_reverse_1(s, 0, (int) strlen(s) - 1);
97+
assert(strcmp("lerhkop tikna", s) == 0);
98+
99+
return 0;
100+
}

Recursion/sum_of_digits.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <assert.h>
2+
3+
long long sum_of_digits(long long n)
4+
{
5+
if (n <= 0) {
6+
return 0;
7+
}
8+
9+
return (n % 10) + sum_of_digits(n / 10);
10+
}
11+
12+
int main()
13+
{
14+
assert(1 == sum_of_digits(1));
15+
assert(3 == sum_of_digits(12));
16+
assert(6 == sum_of_digits(123));
17+
assert(15 == sum_of_digits(12345));
18+
assert(45 == sum_of_digits(123456789));
19+
assert(59 == sum_of_digits(12345678959));
20+
21+
return 0;
22+
}

Recursion/tower_of_hanoi.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
3+
void tower_of_hanoi(int disk, char from, char aux, char to)
4+
{
5+
if (disk <= 1) {
6+
printf("Move disk %d from pole %c to %c\n", disk, from, to);
7+
8+
return;
9+
}
10+
11+
tower_of_hanoi(disk - 1, from, to, aux);
12+
printf("Move disk %d from pole %c to %c\n", disk, from, to);
13+
tower_of_hanoi(disk - 1, aux, from, to);
14+
}
15+
16+
int main()
17+
{
18+
tower_of_hanoi(3, 'A', 'B', 'C');
19+
20+
return 0;
21+
}

0 commit comments

Comments
 (0)