1608. The number-palindrome

 

Check if the given number is a palindrome.

A number is called a palindrome if it reads the same forwards and backwards.

 

Input. One single non-negative 32-bit integer.

 

Output. Print Yes if the number is a palindrome, and No otherwise.

 

Sample input

Sample output

121

Yes

 

 

SOLUTION

char array

 

Algorithm analysis

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

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

·        s[0] = s[6 – 0] = s[6] = ‘4’;

·        s[1] = s[6 – 1] = s[5] = ‘5’;

·        s[2] = s[6 – 2] = s[4] = ‘7’;

 

Algorithm implementation

Store the input number in the character array s.

 

char s[50];

 

Read the input number as a string. Set len 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 i the equality s[i] = s[leni] 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 flag, 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 s.

 

char s[50];

 

The function IsPalindrome checks if the string s 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 s is a palindrome.

 

flag = IsPalindrome(s);

 

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

 

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

 

Algorithm implementation – STL reverse

Read the input number as a string p.

 

cin >> p;

 

Reverse the string p: q = reverse(p).

 

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

 

If the strings p and q 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 a can be obtained using slicing: a[::-1]. Check if the string a is equal to its reverse. Based on the result, print the corresponding answer.

 

if a == a[::-1]:

  print("Yes")

else:

  print("No")