moving elements in arrays

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 <cstring>
#include <stdio.h>
#include <string.h>

using namespace std;

bool isPalindrome(int n)
{
int num = 0, remainder;

int temp = n;
while (temp != 0)
{
remainder = temp%10;
num = num*10 + remainder;
temp /= 10;
}

if (num == n)
{
return true;
}
else
return false;

}

int getShortestLength(int n)
{
char x[40];
strcpy (x, to_string(n));

}

int main()
{
int n;
cout << "Enter a number: ";
cin >> n;
cout << endl;
isPalindrome(n);

cout << endl;
return 0;
}
Last edited on
Copy/move element:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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' ;
    }
}


Is palindrome:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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() ) ;
}
Topic archived. No new replies allowed.