字符串回文在 string.size() 上给出错误答案,但在 string.size()-1 上给出正确答案

String palindrom giving wrong answer on string.size() but right answer on string.size()-1

这是一个程序,它可以找到用户输入的字符串的反转,并判断它是否是回文....现在的问题是,当我在 for 循环中初始化时,它正确地反转了字符串使用 size()-1... 但是当我使用 size() 初始化时它遗漏了一个字符。为什么会这样?

  //palindromee
  #include <iostream>
  using namespace std;
  int main()
  {
   string s;
   string s1;

   cout<<"Enter something:  ";
   cin>>s;
   s1.assign(s);
   int k=0;
   for(int i = s.size()-1  ; i>=0 ; i--)
           //why correct ans reversing on size()-1?
   {       // and wrong ans on size()
        s1[k]=s[i];
        k++;
   }
    cout<<"string s= "<<s;
    cout<<"\nstring s1= "<<s1<<'\n';
    int checker=s.compare(s1);
    if(checker == 0)
    {
        cout<<"\n\npalindrome";
    }
    else cout<<"\n NOT A PALINDROME";
   return 0;

}

那是因为在 C 和 C++ 中数组索引是基于 0 的。字符串 class 也是如此,其中使用索引从 0 开始的 [] 运算符访问单个字符。数组的长度(或字符串的大小)总是在其范围之外。