c++: Problems with my code
#include <iostream> #include <string> int main() { std::string str; std::cin >> str; int count = 0; int i; for (i = 0; str[i] != '\0'; ++i) { count++; } if (count % 2 != 0) { str[i / 2] = ' '; int n = 0; for (int j = 0; j <= i; ++j) { if (str[j] != ' ') { str[n] = str[j]; n++; } } std::cout << str << std::endl; } else { std::cout << str << std::endl; } } In the above code, in line 15, if I write for (int j = 0; j < i; ++j) instead, the last character will be copied once again. For example, if I input "ab12c", the output will be "ab2cc". However, if I write the same code as the above-for (int j = 0; j <= i; ++j). I can get the output that I want. Why adding a "=" will copy the last character again? Even I am the one who writes this code, I still don't know why.XD