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.

  • What kind of "removing spaces" are you talking about? Your code is simply engaged in copying some characters in a string from one place to another. About any removal is not a question. From where can your line know that you wanted to "delete" something? How you are going to solve this problem by introducing a global variable is also not clear. - AnT

2 answers 2

If I understand correctly what you want, then you can shorten the string to the desired length using

 s.resize(n); 

where n is the new length.

You can lengthen :) If you need to lengthen a specific character - specify it as the second argument.

    If you are already using standard algorithms, then you should be consistent, and use algorithms instead of cycles, where it is reasonable.

    To remove spaces from a string, the following common idiom is used.

     #include <string> #include <iterator> #include <algorithm> #include <cctype> //... slovo.erase(std::remove_if(slovo.begin(), slovo.end(), ::isspace), s.end()); 

    To check whether a string is a palindrome, it is sufficient to turn on two algorithm invocations.

     std::transform(slovo.begin(), slovo.end(), slovo.begin(), ::tolower); if (std::equal(slovo.begin(), std::next(slovo.begin(), slovo.size() / 2), slovo.rbegin()) ) { //... } 

    Here is a demo program.

     #include <iostream> #include <string> #include <iterator> #include <algorithm> #include <cctype> int main() { while ( true ) { std::string s; //std::string s("Ab b cB ba"); std::cout << "Enter a sentence (Enter - exit): "; if ( not std::getline( std::cin, s ) or s.empty() ) break; s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end()); std::transform(s.begin(), s.end(), s.begin(), ::tolower); if (std::equal(s.begin(), std::next(s.begin(), s.size() / 2), s.rbegin()) ) { std::cout << "The sentence is a palindrome" << std::endl; } else { std::cout << "The sentence is not a palindromee" << std::endl; } } return 0; } 

    Its console output might look like

     Enter a sentence (Enter - exit): Ab b cB ba The sentence is a palindrome Enter a sentence (Enter - exit): Ab b CcB ba The sentence is a palindrome Enter a sentence (Enter - exit): Ab b CdB b The sentence is not a palindromee Enter a sentence (Enter - exit):