Anyone know how to take the first element in an array and move it to one spot behind end point? For example: '1234' take 1 and move it behind 4 --> '12341'
Im also trying to convert an int array to char array simply cuz my professor said it'd make things easier (hence, to_string).
n is user input. You can actually just ignore my (bool isPalindrome(int n)) for the most part. This all has to be in int getShortestLength(int n).
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
{
// copy the first element to the back of the sequence
std::string str = "1234" ;
if( !str.empty() ) str += str.front() ;
std::cout << str << '\n' ;
}
{
// move the first element to the back of the sequence
std::string str = "1234" ;
std::rotate( str.begin(), str.begin()+1, str.end() ) ; // rotate left by one
std::cout << str << '\n' ;
}
}
#include <string>
#include <algorithm>
#include <cctype>
// remove non-alphanumeric, convert to all lower case
std::string prepare( std::string str )
{
std::string prepared ;
for( char c : str ) if( std::isalnum(c) ) prepared += std::tolower(c) ;
return prepared ;
}
// return true if the string is a plalindrome
bool is_pal_1( std::string str )
{
str = prepare(str) ;
// return true if the string is equal to the reverse of the string
return str == std::string{ str.rbegin(), str.rend() } ;
}
// return true if the string is a plalindrome
bool is_pal_2( std::string str )
{
str = prepare(str) ;
// return true if the left half of the string is equal to the reverse of the right half
return std::equal( str.begin(), str.begin() + str.size()/2, str.rbegin() ) ;
}
// return true if the string is a plalindrome
bool is_pal_3( std::string str )
{
str = prepare(str) ;
std::string cpy = str ; // make a copy
std::reverse( cpy.begin(), cpy.end() ) ; // reverse the copy
// return true if the string is equal to the reverse of the copy of the string
return std::equal( str.begin(), str.begin(), str.rend() ) ;
}