The number-palindrome#1608

The number-palindrome (Editorial)

Read the input number as a string into the character array . Let be the length of the string minus (the index of the last character in the array). The string is a palindrome if for all 0 \le i \le len / 2, the condition s[i] = s[len - i] holds true.

For example, consider the number with length . Then, len = 7 - 1 = 6. The following equalities hold:

Algorithm implementation

Store the input number in the character array .

char s[50];

Read the input number as a string. Set equal to the index of the last digit.

gets(s);
len = strlen(s) - 1;

Initialize flag = 0, which means the number is a palindrome.

flag = 0;

If for some the equality s[i] = s[len - i] is violated, the number is not a palindrome, and we set flag = 1.

for(i = 0; i <= len / 2; i++)
  if (s[i] != s[len - i]) flag = 1;

Based on the value of the variable , print the result.

if (flag == 0) printf("Yes\n"); else printf("No\n");

Algorithm implementation — function IsPalindrome

Store the input number in the character array .

char s[50];

The function IsPalindrome checks if the string is a palindrome.

int IsPalindrome(char *s)
{
  int i = 0, j = strlen(s) - 1;
  while(i < j)
  {
    if (s[i] != s[j]) return 0;
    i++; j--;
  }
  return 1;
}

The main part of the program. Read the input number as a string.

gets(s);

Check if the string is a palindrome.

flag = IsPalindrome(s);

Based on the value of the variable , print the result.

if (flag == 1) printf("Yes\n"); else printf("No\n");

Algorithm implementation — STL reverse

Read the input number as a string .

cin >> p;

Reverse the string : = reverse(p).

q = p; reverse(q.begin(),q.end());

If the strings and are identical, then the input string is a palindrome.

if (p == q) printf("Yes\n"); else printf("No\n");

Java implementation

import java.util.*;

public class Main 
{
  public static void main(String[] args) 
  {
    Scanner con = new Scanner(System.in);
    char[] s = con.nextLine().toCharArray();
    int flag = 0, len = s.length - 1;
    for(int i = 0; i < len - i; i++)
      if (s[i] != s[len - i]) flag = 1;

    if (flag == 0)
      System.out.println("Yes");
    else
      System.out.println("No");    
    con.close();
  }
}

Java implementation — fuction IsPalindrome

import java.util.*;
 
public class Main
{
  public static int IsPalindrome(String s)
  {
    int i = 0, j = s.length() - 1;
    while(i < j)
    {
      if (s.charAt(i) != s.charAt(j)) return 0;
      i++; j--;
    }
    return 1;
  }
  
  public static void main(String[] args)
  {
    Scanner con = new Scanner(System.in);
    String s = con.next();
    
    int flag = IsPalindrome(s);

    if (flag == 1)
      System.out.println("Yes");
    else
      System.out.println("No");
    con.close();
  }
}

Python implementation

Read the input number as a string.

a = input()

The reverse of the string can be obtained using slicing: a[::-1]. Check if the string is equal to its reverse. Based on the result, print the corresponding answer.

if a == a[::-1]: 
  print("Yes")
else: 
  print("No")