// finds first unduplicated char in a string
char Strings::FirstUnduplicated(string str)
{
// hash map to track duplicates
hash_map<char, int> unique;
auto curr = str.begin();
while (curr != str.end())
{
// find duplicates
if (!unique.emplace(*curr, 0).second)
unique[*curr] = 1;
curr++;
}
// find first NON duplicate (== 0)
for (auto const& dup : unique)
if (dup.second == 0)
return dup.first;
return '0';
}
// displays permutations of a string
bool Strings::StringPerms(string& str)
{
auto begin = str.begin();
auto end = str.end();
if (begin == end || begin + 1 == end) return false;
auto first = end - 1;
while (true)
{
auto second = first;
first--;
if (*first < *second)
{
auto third = end;
int test = *first;
while (!(*first < *--third)) /* pass */;
iter_swap(first, third);
reverse(second, end);
return true;
}
if (first == str.begin())
{
reverse(str.begin(), end);
return false;
}
}
}
// find duplicated chars in a string
string Strings::FindDuplicateChars(string str)
{
// map to compare to
hash_map<char, int> dupMap;
// 'unique' string to be returned
string duplicates;
// loop through string
for (auto const& curChar : str)
{
// if emplace fails for first time (== 0)
if (!dupMap.emplace(curChar, 0).second && dupMap[curChar] == 0)
{
// add to 'unique' string
duplicates += string(&curChar, sizeof(char));
// mark as duplicate in map
dupMap[curChar] = 1;
}
}
// return the 'unique' string
return duplicates;
}
// reverse a string (char by char)
void Strings::ReverseString(string::iterator begin, string::iterator end)
{
// stop at center of string
if (begin >= end) return;
// swap chars
char front = *begin;
*begin = *end;
*end = front;
// recurse through the string
ReverseString(++begin, --end);
}
// reverse a string (word by word)
void Strings::ReverseStringofWords(string& str)
{
// reverse the entire string
int begin = 0, end = 0;
ReverseString(str.begin(), str.end() - 1);
// reverse each individual word
while (end++ < str.length())
{
// found end of current word - commented code doesn't work for none letter chars (ie..
'...')
if (/*str[end] == ' ' || */!IsCharAlpha(str[end]) || end == str.length())
{
ReverseString(str.begin() + begin, str.begin() + end - 1);
begin = end + 1;
}
// found more than 1 space in a row
else if (str[begin] == ' ')
begin++;
}
}

Strings

  • 1.
    // finds firstunduplicated char in a string char Strings::FirstUnduplicated(string str) { // hash map to track duplicates hash_map<char, int> unique; auto curr = str.begin(); while (curr != str.end()) { // find duplicates if (!unique.emplace(*curr, 0).second) unique[*curr] = 1; curr++; } // find first NON duplicate (== 0) for (auto const& dup : unique) if (dup.second == 0) return dup.first; return '0'; } // displays permutations of a string bool Strings::StringPerms(string& str) { auto begin = str.begin(); auto end = str.end(); if (begin == end || begin + 1 == end) return false; auto first = end - 1; while (true) { auto second = first; first--; if (*first < *second) { auto third = end; int test = *first; while (!(*first < *--third)) /* pass */; iter_swap(first, third); reverse(second, end); return true; } if (first == str.begin()) { reverse(str.begin(), end); return false; } } } // find duplicated chars in a string string Strings::FindDuplicateChars(string str) { // map to compare to hash_map<char, int> dupMap; // 'unique' string to be returned string duplicates;
  • 2.
    // loop throughstring for (auto const& curChar : str) { // if emplace fails for first time (== 0) if (!dupMap.emplace(curChar, 0).second && dupMap[curChar] == 0) { // add to 'unique' string duplicates += string(&curChar, sizeof(char)); // mark as duplicate in map dupMap[curChar] = 1; } } // return the 'unique' string return duplicates; } // reverse a string (char by char) void Strings::ReverseString(string::iterator begin, string::iterator end) { // stop at center of string if (begin >= end) return; // swap chars char front = *begin; *begin = *end; *end = front; // recurse through the string ReverseString(++begin, --end); } // reverse a string (word by word) void Strings::ReverseStringofWords(string& str) { // reverse the entire string int begin = 0, end = 0; ReverseString(str.begin(), str.end() - 1); // reverse each individual word while (end++ < str.length()) { // found end of current word - commented code doesn't work for none letter chars (ie.. '...') if (/*str[end] == ' ' || */!IsCharAlpha(str[end]) || end == str.length()) { ReverseString(str.begin() + begin, str.begin() + end - 1); begin = end + 1; } // found more than 1 space in a row else if (str[begin] == ' ') begin++; } }