swap first and last character of string in c++

#include <iostream>
#include <string>

void swapFirstAndLast(std::string &str) {
    if (str.length() <= 1)
        return;

    char temp = str[0];
    str[0] = str[str.length() - 1];
    str[str.length() - 1] = temp;
}

int main() {
    std::string input;
    std::cout << "Enter a string: ";
    std::cin >> input;

    swapFirstAndLast(input);

    std::cout << "String after swapping first and last characters: " << input << std::endl;

    return 0;
}