L3B/string_functions.cpp
2024-09-24 16:56:57 -06:00

156 lines
No EOL
3.8 KiB
C++

#include "string_functions.h"
#include <iostream>
using namespace std;
unsigned long string_length(const string STR) {
unsigned long result = -1;
result = STR.length(); // set result to the length of the string
return result;
}
char string_char_at(const string STR, const int IDX) {
char result = '\0';
result = STR.at(IDX);
return result;
}
string string_append(const string LEFT, const string RIGHT) {
string result = LEFT;
result = LEFT + RIGHT;
return result;
}
string string_insert(const string STR, const string TO_INSERT, const int IDX) {
string result = STR;
result.insert(IDX, TO_INSERT);
return result;
}
size_t string_find(const string STR, const char C) {
size_t result = 0;
result = STR.find_first_of(C);
return result;
}
string string_substring(const string STR, const int IDX, const int LEN) {
string result = STR;
result = STR.substr(IDX, LEN);
return result;
}
string string_replace(const string STR, const string TEXT_TO_REPLACE,
const string REPLACE_WITH) {
string result = STR;
auto index = STR.find(TEXT_TO_REPLACE);
if (index != -1ul) {
result.replace(STR.find(TEXT_TO_REPLACE), TEXT_TO_REPLACE.length(),
REPLACE_WITH);
}
return result;
}
string string_first_word(const string STR) {
string result = STR;
result = STR.substr(0, STR.find_first_of(' '));
return result;
}
string string_remove_first_word(const string STR) {
string result = STR;
if (STR.find_first_of(' ') != -1ul) {
result = STR.substr(STR.find_first_of(' ') + 1, STR.length() - 1);
} else {
result = "";
}
return result;
}
string string_second_word(const string STR) {
string result = STR;
result = string_remove_first_word(STR);
result = string_first_word(result);
return result;
}
string string_third_word(const string STR) {
string result = STR;
result = string_remove_first_word(STR);
result = string_remove_first_word(result);
result = string_first_word(result);
return result;
}
string string_nth_word(const string STR, const int N) {
string result = STR;
for (int i = 0; i < N - 1; i++) {
result = string_remove_first_word(result);
}
result = string_first_word(result);
return result;
}
vector<string> string_tokenize(const string STR, const char DELIMINATOR) {
vector<string> result;
result = {""};
for (unsigned long i = 0; i < STR.length(); i++) {
if (STR[i] == DELIMINATOR) {
result.push_back("");
continue;
}
result[result.size() - 1] += STR[i];
}
return result;
}
string string_substitute(const string STR, const char TARGET,
const char REPLACEMENT) {
string result = STR;
while (result.find(TARGET) != -1ul) {
result[result.find(TARGET)] = REPLACEMENT;
}
return result;
}
string string_to_lower(const string STR) {
string result = STR;
for (unsigned int i = 0; i < STR.length(); i++) {
char newChar = STR[i];
if (newChar >= 'A' && newChar <= 'Z') {
newChar = (char)(newChar + 32);
}
result[i] = newChar;
}
std::cout << "TODO: implement string_to_lower(\"" << STR << "\")"
<< std::endl;
return result;
}
string string_to_upper(const string STR) {
string result = STR;
for (unsigned int i = 0; i < STR.length(); i++) {
char newChar = STR[i];
if (newChar >= 'a' && newChar <= 'z') {
newChar = (char)(newChar - 32);
}
result[i] = newChar;
}
std::cout << "TODO: implement string_to_upper(\"" << STR << "\")"
<< std::endl;
return result;
}
int string_compare(const string LHS, const string RHS) {
int result = 0;
if (LHS > RHS) {
result = 1;
} else {
if (LHS < RHS) {
result = -1;
}
}
std::cout << "TODO: implement string_compare(\"" << LHS << "\", \"" << RHS
<< "\")" << std::endl;
return result;
}