forked from ChicoState/UnitTestPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice.cpp
More file actions
85 lines (75 loc) · 2.05 KB
/
Practice.cpp
File metadata and controls
85 lines (75 loc) · 2.05 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
#include "Practice.h"
#include <string>
using std::string;
// Receive three integers and rearrange their values so that they are in
// descending order from greatest (first) to least (third)
void Practice::sortDescending(int & first, int & second, int & third)
{
//solution gotten from
// https://stackoverflow.com/questions/4367745/simpler-way-of-sorting-three-numbers
//used the swap method and switched > for <
//and wrote out the swap function rather than using the built in swap method
if (first < third)
{
int temp = first;
first = third;
third = temp;
}
if (first < second)
{
int temp = first;
first = second;
second = temp;
}
if (second < third)
{
int temp = second;
second = third;
third = temp;
}
}
// Receive a string and return whether or not it is strictly a palindrome,
// where it is spelled the same backwards and forwards when considering every
// character in the string, but disregarding case ('x' is the same as 'X')
bool Practice::isPalindrome(string input)
{
for(unsigned int i=0; i < input.size(); i++)
{
if( input[i] < 'A' || input[i] > 'Z' )
{
//change lower case to upper case
input[i] = input[i] - ('a' - 'A');
}
}
bool match = false;
if (input.length() == 0) {
match = true; //according to google, empty string is a palindrome
}
else {
for(unsigned int i=0; i < input.size()/2; i++)
{
if( input[i] == input[input.size()-1-i] )
match = true;
else
match = false;
}
}
return match;
}
// This function receives a string and counts how many times the same character
// is repeated at the beginning of the string, before any other characters. The
// function is case sensative so 'Z' is different than 'z'.
int Practice::count_starting_repeats(string word)
{
int repetition = 0;
int index = 0;
char letter;
if( word.length() > 0 )
letter = word[0];
for(unsigned int i=1; i < word.length(); i++){
if( word[i] == letter ){
repetition++;
}
}
return repetition;
}