-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercises.cpp
More file actions
92 lines (81 loc) · 1.84 KB
/
exercises.cpp
File metadata and controls
92 lines (81 loc) · 1.84 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <cmath>
using namespace std;
void fib();
void fib(int num1, int num2);
void fizzbuzz();
void isPali(string text);
string toLower(string text);
string reverse(string text);
int main() {
fib();
fizzbuzz();
isPali("racecar");
return 0;
}
// fibonacci sequence
void fib() {
fib(0, 1);
}
void fib(int num1, int num2) {
if (num2 < 1000000) {
cout << num1 << ", ";
fib(num2, (num1 + num2));
} else {
cout << num1 << ", " << num2 << endl;
}
}
// fizzbuzz
void fizzbuzz() {
for (int i = 1; i < 30; i++) {
if (i % 3 == 0 && i % 5 == 0) {
cout << "FizzBuzz, ";
} else if (i % 3 == 0) {
cout << "Fizz, ";
} else if (i % 5 == 0) {
cout << "Buzz, ";
} else {
cout << i << ", ";
}
}
cout << "Buzz" << endl;
}
// palindrome check
void isPali(string text) {
string lowerText = toLower(text);
int length = lowerText.length();
if (length % 2 == 0) {
if (lowerText.substr(0, length / 2) == reverse(lowerText.substr(length / 2))) {
cout << true << endl;
} else {
cout << false << endl;
}
} else {
if (lowerText.substr(0, length / 2) == reverse(lowerText.substr((length / 2) + 1))) {
cout << true << endl;
} else {
cout << false << endl;
}
}
}
// converts a string to lowercase
string toLower(string text) {
string lower;
for (char c : text) {
lower += tolower(c);
}
return lower;
}
// reverses a string
string reverse(string text) {
string reverseText;
for (int i = text.length() - 1; i >= 0; i--) {
reverseText += text[i];
}
return reverseText;
}