Coding Education STEM Uncategorized

Let’s Play Palindrome! (C/C++)

Did you know that the longest Palindromic word in the Oxford English Dictionary is “tattarrattat” coined by James Joyce in Ulysses (1922) meaning “a knock on the door”?

What is a Palindrome, you ask? Well, you already know it or have seen it. According to Wikipedia:
“A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers.”

So, “Amma” is same spelled backward “ammA” (remember, capital letters don’t matter). So, it’s a Palindrome. “ABBa” is too. “111” is too. “123” is not.

How would you test if a string (again, letters or numbers) if a Palindrome through your own function/code? Here I’ll show you (with explanation, not just for memorization or copy/paste) how to do it in C.

Logic:

The idea simply is to compare the string (word or a number), and compare it to its reversed version. Reversing “bob” gives us “bob”, so it’s a Palindrome. Reversing “bobs” gives us “sbob” so obviously it’s NOT a Palindrome.

Here’s the pseudocode in C/C++:

  • get input from user: using gets() function and store it in a variable (e.g. a)
  • Copy it to a new variable string b using strcpy() and reverse the copied one using strrev()
  • Compare the original input a with the reversed variable b. If they’re equal, it’s a Palindrome, other it is not…using strcmp()

Okay, but as with Python, what if the entry was blank (a space, or just Enter)? Or just one character such as “1” or “a”…of course, reverse of the same would be the same! I don’t like that at all, so I check for those by checking the length of the input using len() function…so here’s the additional logic:

  • if the string is empty/blank or only 1 character, then don’t check, tell user that it should be more than that; otherwise do the above procedure.

To make it more interactive, I put the whole code in a “forever” loop using while(1) and only exit if the user enters a certain string, which I decided to be “EXIT” (case-insensitive).

We are done! So, to tie it all altogether, 1) get the input “forever until exit” 2) if the string is not at least 2 characters, show a message and quit 3) otherwise, compare and tell user if it’s a Palindrome or not.

The code now is this

(be sure to include these at top:

#include <stdio.h>
#include <string.h>

and if you’re using C++ syntax also add this:

#include <iostream>
int main(int argc, char** argv) 
{
  char a[100], b[100];
 
 while (1)
 {
 printf("Enter a string to check if it is a palindrome. (type EXIT to end)\n");
 gets(a);
 if (strcmp(a, "EXIT") == 0 || (strcmp(a, "exit")==0))
   return 0;
 strcpy(b, a);  // Copying input string
 strrev(b);  // Reversing the string
 if (strcmp(a, b) == 0 && (strlen(a)>1))  // Comparing input string with the reverse string
    printf("The string is a PALINDROME.\n");
 else
    printf("Not a palindrome.\n");
} 

return 0;

} 

So, go ahead and try your strings! Numbers or letters and it'll ALWAYS give you the right answer.

If you want to see the same implementation in Python language, check out my blog here.

 

One thought on “Let’s Play Palindrome! (C/C++)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top