Added functions to check palindromes and convert to bin

This commit is contained in:
2021-06-29 10:17:41 -04:00
parent 07d32e6b42
commit 721128a276
2 changed files with 127 additions and 18 deletions

View File

@@ -30,6 +30,7 @@
#include <cmath>
#include <unordered_map>
#include <map>
#include <bitset>
namespace mee{
@@ -93,6 +94,11 @@ template <class T>
T findMax(const std::vector<T>& ary);
//This function returns the number of times the character occurs in the string
int findNumOccurrence(std::string str, char ch);
//This function returns true if the string passed in is a palindrome
bool isPalindrome(std::string str);
//This function converts a number to its binary equivalent
template <class T>
std::string toBin(T num);
template <class T>
@@ -586,6 +592,33 @@ T factorial(T num){
return fact;
}
//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 converts a number to its binary equivalent
template <class T>
std::string toBin(T num){
//Convert the number to a binary string
std::string fullString = std::bitset<sizeof(T)>(num).to_string();
//Remove leading zeros
int loc = 0;
for(loc = 0;(loc < fullString.size()) && (fullString[loc] == '0');++loc);
std::string trimmedString = fullString.substr(loc);
if(trimmedString == ""){
trimmedString = "0";
}
return trimmedString;
}
}