DAVID21-EDITORIAL

PROBLEM LINK:

Practice : David and his Words | CodeChef

Author: RAKSHIT NAYAK
Tester: RAKSHIT NAYAK
Editorialist: RAKSHIT NAYAK

DIFFICULTY:

CAKE WALK

PREREQUISITES:

Character Arrays.

PROBLEM:

David a 10 year kid is reading a book , He came across some words which read from back gives the same word when read from front. He became curios and started searching similar words and also got to know that these words are called as palindrome. He now asked his mom to ask weather a word S, is palindrome and said i will answer true or false.!! Output the same .

Quick Explanation:

Hope you have read the problem statement in practice section so, first we need to input the size of the string, and the second line takes input as string with size mentioned in first line.Now our task is to print true or false evaluating the given string is palindrome or not.
if the string is palindrome print true stating it is a palindrome else print false stating it is not palindrome

Approach:

  1. Let the length of the character array be n.
  2. Keep a boolean variable ans to store the result and initialize it with true.
  3. Iterate over the string and check if i th character is equal to (n-i-1) th
    there can be 2 cases
    a) If equal, then do nothing
    b) If unequal, then put ans = false
  4. When the loop ends, if ans is true, then the string is palindrome else it is not a
    palindrome.

SOLUTION:

Setter's Solution
#include <iostream>
using namespace std;
 
int main()
{
    int n;
    cin >> n;
    char arr[n + 1];
    cin >> arr;
 
    bool check = 1;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] != arr[n - 1 - i]) //checking if arr[i] is equal when read from reverse direction
        {
            check = 0;
            break;
        }
    }
    if (check == true)
    {
        cout << "true " << endl;
        cout << "it is a palindrome" << endl;
    }
    else
    {    
        cout << "false" << endl;
        cout << "it is not palindrome" << endl;
    }
 
    return 0;
}

Well this was my approach, feel free to share your approach here. Suggestions are always welcomed. :smile: