mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added functions to check palindromes and convert to bin
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user