L3B/string_functions.cpp

144 lines
4.8 KiB
C++
Raw Normal View History

#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);
std::cout << "TODO: implement string_char_at(\"" << STR << "\", " << IDX << ")" << std::endl;
return result;
}
string string_append(const string LEFT, const string RIGHT) {
string result = LEFT;
result = LEFT + RIGHT;
std::cout << "TODO: implement string_append(\"" << LEFT << "\", \"" << RIGHT << "\")" << std::endl;
return result;
}
string string_insert(const string STR, const string TO_INSERT, const int IDX) {
string result = STR;
result.insert(IDX, TO_INSERT);
std::cout << "TODO: implement string_insert(\"" << STR << "\", \"" << TO_INSERT << "\", " << IDX << ")" << std::endl;
return result;
}
size_t string_find(const string STR, const char C) {
size_t result = 0;
result = STR.find_first_of(C);
std::cout << "TODO: implement string_find(\"" << STR << "\", '" << C << "')" << std::endl;
return result;
}
string string_substring(const string STR, const int IDX, const int LEN) {
string result = STR;
result = STR.substr(IDX, LEN);
std::cout << "TODO: implement string_substring(\"" << STR << "\", " << IDX << ", " << LEN << ")" << std::endl;
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);
}
std::cout << "TODO: implement string_replace(\"" << STR << "\", \"" << TEXT_TO_REPLACE << "\", \"" << REPLACE_WITH << ")\"" << std::endl;
return result;
}
string string_first_word(const string STR) {
string result = STR;
result = STR.substr(0, STR.find_first_of(' '));
std::cout << "TODO: implement string_first_word(\"" << STR << "\")" << std::endl;
return result;
}
string string_remove_first_word(const string STR) {
string result = STR;
// TODO 08: set result to be the string with the first word removed
std::cout << "TODO: implement string_remove_first_word(\"" << STR << ")\"" << std::endl;
return result;
}
string string_second_word(const string STR) {
string result = STR;
// TODO 09: set result to be the second word from the string
std::cout << "TODO: implement string_second_word(\"" << STR << "\")" << std::endl;
return result;
}
string string_third_word(const string STR) {
string result = STR;
// TODO 10: set result to be the third word from the string
std::cout << "TODO: implement string_third_word(\"" << STR << "\")" << std::endl;
return result;
}
string string_nth_word(const string STR, const int N) {
string result = STR;
// TODO 11: set result to be the nth word from the string
std::cout << "TODO: implement string_nth_word(\"" << STR << "\", " << N << ")" << std::endl;
return result;
}
vector<string> string_tokenize(const string STR, const char DELIMINATOR) {
vector<string> result;
// TODO 12: split the string by the given deliminator
std::cout << "TODO: implement string_tokenize(\"" << STR << "\", '" << DELIMINATOR << "')" << std::endl;
return result;
}
string string_substitute(const string STR, const char TARGET, const char REPLACEMENT) {
string result = STR;
// TODO 13: set result to be the string with all instances of TARGET replaced
std::cout << "TODO: implement string_substitute(\"" << STR << "\", '" << TARGET << "', '" << REPLACEMENT << "')" << std::endl;
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;
}