The problem is that when shortening a line in the code, that is, after removing the spaces, the length of the array, the resulting lenght remains unchanged. The question is, is it necessary to create a new array, or is it possible to somehow shorten its original size?
#include "stdafx.h" using namespace std; int YANEXOTEL; string np(string slovo) { int dlina = slovo.length(); for (int i = 0; i != dlina - 1; i++) { if (slovo[i] == ' ') { for (auto symb = i; symb != dlina; symb++) { slovo[symb] = slovo[symb + 1]; } dlina = dlina - 1; } } return slovo; } int main(int arc, char *argv[]) { string slovo; setlocale(LC_ALL, "Russian"); SetConsoleCP(1251); SetConsoleOutputCP(1251); cout << "Введите фразу на проверку: \n"; getline(cin,slovo); cout << "Ваша фраза - " << slovo << "\n"; transform(slovo.begin(), slovo.end(), slovo.begin(), tolower); slovo=np(slovo); int dlina=slovo.size(); cout << "Измененная фраза - " << slovo << "\n"; cout << "Длина - " << dlina << "\n"; for (int i = 0; i != dlina-1; i++) { if (slovo[i] != slovo[(dlina-1) - i]) { cout << "Слово не то самое\n"; system("pause"); return 0; } } cout << "Слово то самое\n"; system("pause"); return 0; } The problem can be solved by simply entering a global variable, but I don’t really like using it.