Open In App

R Program to Check if a String is a Palindrome

In this article, we explore a simple yet essential task in programming: checking whether a given string is a palindrome. A palindrome is a sequence of characters that reads the same forwards and backwards, making it a common problem in text processing and string manipulation. We’ll delve into the logic and implementation of a program in the R programming language to efficiently determine if a string exhibits this intriguing property, offering practical insights for both beginners and experienced programmers.

Palindrome

Palindrome, well it’s a word, phrase, number, or sequence that reads the same forwards and backwards. For instance, “racecar” and “madam” are palindromes, but “hello” and “12345” are not.



Examples:

Input: ‘madam’



Output: YES

Input : ‘hello’

Output: NO

Concepts related to the topic:

Native Approach

One of the simplest ways to check for a palindrome in R is by comparing the original string with its reverse.




# Function to check if a string is a palindrome
isPalindrome <- function(s) {
  s <- tolower(s)
  s <- gsub(" ", "", s)
  rev_s <- paste(rev(unlist(strsplit(s, ""))), collapse = "")
  return(s == rev_s)
}
 
# Predefined input (change this string as needed)
user_input <- "malayalam"
# user_input<- "mala yalam"  # this also give same output as previous because both are plaindrome
 
# Check if the user input is a palindrome and print YES or NO
if (isPalindrome(user_input)) {
  cat("YES\n")
} else {
  cat("NO\n")
}

Output:

YES

We convert the string to lowercase and remove spaces for a case-insensitive and space-agnostic comparison. Then, we check if the string is equal to its reverse.

Iterative Method

This method involves comparing characters from the start and end of the string, working towards the center. If all characters match, the string is a palindrome. Here’s the code:




# Function to check if a string is a palindrome
isPalindrome <- function(s) {
  s <- tolower(s)
  s <- gsub(" ", "", s) 
  n <- nchar(s)
   
  for (i in 1:(n %/% 2)) {
    if (substr(s, i, i) != substr(s, n - i + 1, n - i + 1)) {
      return(FALSE)
    }
  }
  return(TRUE)
}
 
# Predefined input (change this string as needed)
user_input <- "nitin"
 
# Check if the user input is a palindrome and print YES or NO
if (isPalindrome(user_input)) {
  cat("YES\n")
} else {
  cat("NO\n")
}

Output:

YES

Using an Extra Variable

This method involves storing characters in an empty variable and comparing it with the original string. We reverse the string by building it character by character in reverse order and then compare it to the original.




# Function to check if a string is a palindrome
isPalindrome <- function(s) {
  s <- tolower(s)
  s <- gsub(" ", "", s) 
  w <- ""
  for (i in s) {
    w <- paste(i, w, sep = "")
  }
  return(s == w)
}
 
# Predefined input (change this string as needed)
user_input <- "madam"
 
# Check if the user input is a palindrome and print YES or NO
if (isPalindrome(user_input)) {
  cat("YES\n")
} else {
  cat("NO\n")
}

Output:

YES

Using Inbuilt Functions

R provides convenient inbuilt functions to reverse strings. We use rev() and strsplit() to reverse the string and then compare it with the original.




# Function to check if a string is a palindrome
isPalindrome <- function(s) {
  s <- tolower(s) 
  s <- gsub(" ", "", s)
  rev_s <- paste(rev(strsplit(s, NULL)[[1]]), collapse = "")
  return(s == rev_s)
}
 
# Predefined input (change this string as needed)
user_input <- "13431"
 
# Check if the user input is a palindrome and print YES or NO
if (isPalindrome(user_input)) {
  cat("YES\n")
} else {
  cat("NO\n")
}

Output:

YES

Article Tags :