Sunday, March 13, 2016

Palindrome Checker in C++

A computer has been around for us for a long period of time. We use computers is a wide range of applications such as in business, industry, education, entertainment, weather forecasting, military for example.  It makes our lives easier because most of the tedious task and difficult task is being done by the computer with the use of a computer program. As a programmer our work is very crucial and critical most of the time because we are dealing with important facts and figures all the time one small mistake it can cause tremendous amount of losses on the part of the company or even in an organization we are working with.

Computers at first is designed to perform computations during world war 2 but later years it has been used to manipulate other types of date such as words, sentences, phrase or even paragraphs. In this article I will explain and discuss in detailed one of the most basic string applications written in C++ programming language a program called Palindrome Checker. C++ is one of the most versatile programming language derive from the C language that is introduced in 1972 by Dr. Dennis Ritchie later one as the complexity in software development particularly in system programming. C++ is being developed by Dr. Bjarne Stroustrup to overcome some of the weak point of the C language in 1981 at AT and T laboratories in New Jersey.  The enhancement of C++ library it gives the language more suitable application not only limited in system programming. When we talking about system programming we are referring to writing programs that interacts to the computer hardware such examples is operating systems, compilers and interpreters, antivirus software and system tools to maintain the normal operation of our computers.

So the question is what is a palindrome well a palindrome it refers to word, sentences, phrases, numbers or string when we read it forward and backward the spelling and pronunciation is the same. Example of words that is palindrome by nature is ana, radar, ama, abba to name a few.  In this article in writing our C++ program I will be using CodeBlock text editor and Dev C++ that is already integrated in our CodeBlocks editor.  This two software is open source it means it is free to use and download over the Internet for your own personal use and commercial use also.  You can download CodeBlock in their web address http://www.codeblocks.org/ .

Let us presuming you have already downloaded and installed codeblocks in your computer. The first step is to open our editor and encode the C++ commands. In this program we declare to library file in C++ that standard iostream and string.h. The iostream is one of the very basic library file we use every time we write the C++ program it tells our compiler that we are allowed to use basic input and output commands in C++ such for example cout and cin commands. The next library file that I will discuss is the string.h this library file in C++ is used primarily to include string functions in our program in this program we use the strcpy, strcmpi and strrev functions. The strcpy is a function that will copy the first and second  string variable in our program. The second strcpy command will copy the variable word3 that will act as your temporary folder of a string and  we use the strrev this function will reverse the first string variable in our program. The next part is the use of the  check_same_string = strcmpi(word1,word3); this command tells use the check_same_string is our assignment operator that will hold the compared string of word1 and word3 variables.  In our condition in our program  if (check_same_string == 0) it means that is the both string word1 and word3 are the same they are considered as palindrome if not the word is not a palindrome. Below is the complete program listing of our palindrome checker program.


If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.






Sample Program Output


#include <iostream>
#include <string.h>

  using namespace std;


char word1[50],word2[50],word3[50];

int check_same_string=0;

main()
{
    system("cls");
    cout << "\n\n";
    cout << "  ***====Palindrome Checker ====***";
    cout << "\n\n";
    cout << "Enter a Word Please : ";
    gets(word1);
     strcpy(word2,word1);
     strcpy(word3,strrev(word2));
     check_same_string = strcmpi(word1,word3);
      if (check_same_string == 0)

        {
            cout << "\n\n";
            cout << "The word in reversed order is " << word3 << ". \n";
            cout << "It is a Palindrome.";
        }
      else {
            cout << "\n\n";
            cout << "The word in reversed order is " << word3 << ".\n";
            cout << "It is Not a Palindrome.";
        }
     cout << "\n\n\n";
     cout << "\t  Thank You For Using This Program";
     cout << "\n\n";
     system("pause");
}





No comments:

Post a Comment