Quick actions

cmd+k|ctrl+k

Navigation

Languages

check elements on string to see if palindrome

Snippet info

Language

Cpp

Visibility

public

Author

aventus

Created

2019-05-13T19:46:44Z

Updated

2019-05-13T20:26:01Z

#include <iostream>
using namespace std;
int main ()
{
        string word;
        int wordsize;

        // use getline, not cin >> because using strings 
        cout << "Please enter a word." << endl;
        getline (cin, word);

        wordsize = word.length();

        cout << "The length of the word is " << wordsize << "." << endl;

        
        for (int i=0; i < wordsize/2 ; i++)
        {


                // if statement for word if equal, break loop and return YES
                if (word[i]==word[wordsize-i-1])
                {
                        cout << "Checking element " << i << " with element " << wordsize - i -1 << "." << endl;
                }
                
                // if statement for word not equal, returns NO
                if (word[i]!=word[wordsize-i-1])
                {
                        cout << "Checking element " << i << " with element " << wordsize - i -1 << "." << endl;
                        cout << "NO: it is not a palindrome." << endl;
                        return 0;
                }




        }

                cout << "YES: it is a palindrome." << endl;

        return 0;
}
INFO