why does the code not work if the string is inputted as string not as an array? [closed]

  c++, palindrome

Here I could have used char string1[] and it would have worked

#include <iostream>
    #include<cstring>
    using namespace std;
 

    
    int main(){
        string string1;
        int i, length;
        int flag = 0;
    
        cout << "Enter a string: "; cin >> string1;
    
        length = string1.size();
    
        for(i=0;i < length ;i++){
            if(string1[i] != string1[length-i-1]){
                flag = 1;
                break;
       }
    }
    
        if (flag) {
            cout << string1 << " is not a palindrome" << endl;
        }
        else {
            cout << string1 << " is a palindrome" << endl;
        }
     
      
    }

Source: Windows Questions C++

LEAVE A COMMENT