//myClasses/headers/mee/stringAlgorithms.hpp
//Matthew Ellison
// Created: 07-02-21
//Modified: 10-11-21
//This file contains declarations of functions I have created to manipulate strings
/*
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
*/
#pragma once
#include
#include
namespace mee{
//This is a function that creates all permutations of a string and returns a vector of those permutations.
std::vector getPermutations(std::string master, int num = 0){
std::vector perms;
//Check if the number is out of bounds
if((num >= master.size()) || (num < 0)){
return perms;
}
//If this is the last possible recurse just return the current string
else if(num == (master.size() - 1)){
perms.push_back(master);
return perms;
}
//If there are more possible recurses, recurse with the current permutation
std::vector temp;
temp = getPermutations(master, num + 1);
perms.insert(perms.end(), temp.begin(), temp.end());
//You need to swap the current letter with every possible letter after it
//The ones needed to swap before will happen automatically when the function recurses
for(int cnt = 1;(num + cnt) < master.size();++cnt){
std::swap(master[num], master[num + cnt]);
temp = getPermutations(master, num + 1);
perms.insert(perms.end(), temp.begin(), temp.end());
std::swap(master[num], master[num + cnt]);
}
//The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning
if(num == 0){
std::sort(perms.begin(), perms.end());
}
return perms;
}
//This function returns the number of times the character occurs in the string
int findNumOccurrence(std::string str, char ch){
int num = 0; //Set the number of occurrences to 0 to start
//Loop through every character in the string and compare it to the character passed in
for(char strCh : str){
//If the character is the same as the one passed in increment the counter
if(strCh == ch){
++num;
}
}
//Return the number of times the character appeared in the string
return num;
}
//Return a vector of strings split on the delimiter
std::vector split(std::string str, char delimiter){
std::vector splitStrings;
int location = 0;
location = str.find(delimiter);
while(location != std::string::npos){
//Split the string
std::string firstString = str.substr(0, location);
str = str.substr(location + 1); //+1 to skip the delimiter itself
//Add the string to the vector
splitStrings.push_back(firstString);
//Get the location of the next delimiter
location = str.find(delimiter);
}
//Get the final string if it isn't empty
if(!str.empty()){
splitStrings.push_back(str);
}
//Return the vector of strings
return splitStrings;
}
//This function returns true if the string passed in is a palindrome
bool isPalindrome(std::string str){
std::string rev = str;
std::reverse(rev.begin(), rev.end());
if(str == rev){
return true;
}
else{
return false;
}
}
//This function returns true if the string passed to it is a pandigital
bool isPandigital(std::string str, char bottom, char top){
//Return false if top < bottom
if(top < bottom){
return false;
}
//Return false if the wrong number of characters are in the string
if(str.size() != (top - bottom + 1)){
return false;
}
//Make sure that all of the needed characters are in the string exactly one time
for(char cnt = bottom;cnt <= top;++cnt){
//If a single character is found more than once then the string is not pandigital
if(findNumOccurrence(str, cnt) != 1){
return false;
}
}
//If the function has reached this part it has passed all of the falsifying tests
return true;
}
bool isPandigital(std::string str){
return isPandigital(str, '1', '9');
}
}