So I've been working on a program that invert it's characters. And I've doing it by string.length() and string.at(). But, it occur error "out of range" even though it's not out of range (I've checked it by print the pos of at variabele) I suspected it's caused of data types mismatch. Correct me if I'm wrong. This is my code.
#include "pch.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
string compose(string temp) {
size_t temp1 = 0, temp2 = temp.length() - 1;
string temporary, temporary1;
cout << temp2;
while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
temporary1 = temp.at(temp1);
temporary = temp.at(temp2);
temp.replace(temp1, 1, temporary);
temp.replace(temp2, 1, temporary1);
temp1++;
temp2--;
}
return temp;
}
int main()
{
cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjjnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
_getch();
return 0;
}