Repository navigation
Expand file tree
/
Copy pathstrings.cpp
More file actions
100 lines (88 loc) · 3.49 KB
/
Copy pathstrings.cpp
File metadata and controls
100 lines (88 loc) · 3.49 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
93
94
95
96
97
98
99
100
// Strings: C-style character arrays vs std::string, plus classic problems:
// palindromes, vowel counting, reversing words, title case,
// find & replace and character frequency.
#include <cctype>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
int myStrlen(const char* s) {
int len = 0;
while (s[len] != '\0') ++len; // C-strings end with the null character
return len;
}
bool isPalindrome(const std::string& text) {
std::string cleaned; // keep only letters/digits, lower-cased
for (char c : text) {
unsigned char uc = static_cast<unsigned char>(c);
if (std::isalnum(uc)) cleaned += static_cast<char>(std::tolower(uc));
}
return cleaned == std::string(cleaned.rbegin(), cleaned.rend());
}
int countVowels(const std::string& s) {
int count = 0;
for (char c : s) {
switch (std::tolower(static_cast<unsigned char>(c))) {
case 'a': case 'e': case 'i': case 'o': case 'u':
++count;
break;
default:
break;
}
}
return count;
}
std::string reverseWords(const std::string& sentence) {
std::istringstream in(sentence);
std::string word, result;
while (in >> word) result = result.empty() ? word : word + " " + result;
return result;
}
std::string toTitleCase(std::string s) {
bool startOfWord = true;
for (char& c : s) {
unsigned char uc = static_cast<unsigned char>(c);
if (std::isspace(uc)) {
startOfWord = true;
} else if (startOfWord) {
c = static_cast<char>(std::toupper(uc));
startOfWord = false;
}
}
return s;
}
std::string replaceAll(std::string text, const std::string& from, const std::string& to) {
std::size_t pos = 0;
while ((pos = text.find(from, pos)) != std::string::npos) {
text.replace(pos, from.length(), to);
pos += to.length();
}
return text;
}
std::map<char, int> charFrequency(const std::string& s) {
std::map<char, int> freq; // std::map keeps keys sorted
for (char c : s) ++freq[c];
return freq;
}
int main() {
char greeting[] = "Hello";
std::cout << "C-string \"" << greeting << "\" has length " << myStrlen(greeting)
<< " but occupies " << sizeof(greeting) << " bytes (the extra one is '\\0')\n\n";
std::string sentence = "programming fundamentals build strong problem solving skills";
std::cout << "Sentence: " << sentence << '\n';
std::cout << "Length: " << sentence.length() << '\n';
std::cout << "Vowels: " << countVowels(sentence) << '\n';
std::cout << "Words reversed: " << reverseWords(sentence) << '\n';
std::cout << "Title case: " << toTitleCase(sentence) << '\n';
std::cout << "substr(0, 11): " << sentence.substr(0, 11) << '\n';
std::cout << "find(\"strong\"): " << sentence.find("strong") << "\n\n";
std::string phrases[] = {"A man, a plan, a canal: Panama", "Hello World", "Was it a car or a cat I saw?"};
for (const std::string& p : phrases)
std::cout << '"' << p << "\" -> " << (isPalindrome(p) ? "palindrome" : "not a palindrome") << '\n';
std::string text = "the cat sat on the mat with the hat";
std::cout << "\nReplace \"the\" with \"a\": " << replaceAll(text, "the", "a") << '\n';
std::cout << "\nCharacter frequency in \"mississippi\":\n";
for (const auto& [ch, count] : charFrequency("mississippi"))
std::cout << " " << ch << ": " << count << '\n';
return 0;
}