consolidate all repos to one for archive
This commit is contained in:
74
semester_2/programiranje_2/naloga0302/TextUtility.cpp
Normal file
74
semester_2/programiranje_2/naloga0302/TextUtility.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "TextUtility.h"
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
|
||||
std::string TextUtility::capitalize(const std::string &str) {
|
||||
std::string ret;
|
||||
bool nex = false;
|
||||
auto it = str.begin();
|
||||
ret.push_back(std::toupper(*it));
|
||||
it++;
|
||||
while (it != str.end()) {
|
||||
if (nex) {
|
||||
ret.push_back(std::toupper(*it));
|
||||
nex = false;
|
||||
} else {
|
||||
ret.push_back(*it);
|
||||
}
|
||||
if ('.' == *it || '!' == *it || '?' == *it) nex = true;
|
||||
it++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string TextUtility::toUpperCase(const std::string &str) {
|
||||
std::string ret;
|
||||
auto it = str.begin();
|
||||
while (it != str.end()) {
|
||||
ret.push_back(std::toupper(*it));
|
||||
it++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool TextUtility::isNumeric(const std::string &str) {
|
||||
auto it = str.begin();
|
||||
while (it != str.end() && std::isdigit(*it)) ++it;
|
||||
return !str.empty() && it == str.end();
|
||||
}
|
||||
|
||||
int TextUtility::contains(const std::string &str, const std::string &substr) {
|
||||
std::size_t found = str.find(substr);
|
||||
if (found != std::string::npos) return found;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string TextUtility::addSpaces(const std::string &str) {
|
||||
std::string ret;
|
||||
auto it = str.begin();
|
||||
while (it != str.end()) {
|
||||
ret.push_back(*it);
|
||||
ret.push_back(' ');
|
||||
it++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string TextUtility::removeDuplicatedSpaces(const std::string &str) {
|
||||
std::string ret;
|
||||
bool next = false;
|
||||
auto it = str.begin();
|
||||
while (it != str.end()) {
|
||||
if (next) {
|
||||
if(*it != ' ') {
|
||||
ret.push_back(*it);
|
||||
}
|
||||
next = false;
|
||||
} else {
|
||||
ret.push_back(*it);
|
||||
}
|
||||
if (*it == ' ') next = true;
|
||||
it++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user