forked from Daniel-Calderon/team_strings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lomeli_teamString.cpp
108 lines (95 loc) · 3.03 KB
/
Lomeli_teamString.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Name: Noe Lomeli
// Date: October 8, 2015
// Project: Team Strings
#include <iostream>
#include <string>
#include <cassert>
#include <cctype>
using namespace std;
//
// team_strings HW assignment
// Fully implement the functions below, incorporating asserts as appropriate to test.
// // Name your C++ file: lastName_teamString.cpp
//
//
string initials(string strA);
// receives a string comprising first, middle and last name and returns the initials of each name, capitalized
// TEST: Maria Sera Turing returns MST
int countAlpha(string strA);
// receives a string comprising a sentence, counts and returns the number of alphabetical
// characters a -> z and A -> Z are the alphabetical characters.
int countWords(string strA);
// receives a string comprising a sentence, counts and returns the number of words in a the sentence.
// words are separated by spaces.
bool isPalindrome(string strA);
// receives a string and returns true if the string is a palindrome
// a one character string will be considered a palindrome
int main()
{
assert(initials("maria sera turing") == "MST");
assert(initials("andrea Marcela gomez") == "AMG");
assert(initials("Ben Andrew Lopez") == "BAL");
assert(countAlpha("Ben Andrew 234325#$@5! ") == 9);
assert(countAlpha("ABCabc123!@#") == 6);
assert(countAlpha("234%^&*123!@#") == 0);
assert(countWords("one two three four") == 4);
assert(countWords("one two") == 2);
assert(countWords("one") == 1);
assert(isPalindrome("racecar") == 1);
assert(isPalindrome("rats live on no evil star") == 1);
assert(isPalindrome("Deed") == 0);
cout << "It works!"<< endl;
return 0;
}//main
string initials(string strA)
{
int i = 0;
string initials = strA.substr(0,1);//this is the first initial
initials = toupper(initials[0]);//makes first initial a capital
for(int ix(1); ix < strA.length(); ix++)
{
if (strA[ix] == ' ')
{
initials += toupper(strA[ix+1]);//adds the other initials already capitalized
}//if
}//for
return initials;
}//string initials
int countAlpha(string strA)
{
int sum = 0;
for(int ix(0); ix < strA.length(); ix++)
{
if((strA[ix] >= 65) && (strA[ix] <= 90) || (strA[ix] >= 97) &&(strA[ix] <= 122))//this checks if there is a letter or not
{
sum ++;
}//if
}//for
return sum;
}//countAlpha
int countWords(string strA)
{
int count = 0;
for (int ix(0); ix < strA.length(); ix++)
{
if (strA[ix] == ' ')//when it finds a space it will add to the count
{
count ++;
}//if
}//for
return count + 1;
}//countWords
bool isPalindrome(string strA)
{
string strB;
for (int ix(strA.length()-1); ix >= 0; ix--)//the string will be spelled from end to beginning
{
strB += strA[ix];
}//for
if (strA == strB)
{
return true;
}//if
else
return false;
} //bool isPalindrome(string strA)