C/C++ program to check the Palindrome string.

Palindrome logo

Palindrome string:

A string or word which are same from the start and end means word or program which look like same as original and reversed.

To do that we have to first reverse the string.


To write the program of the Palindrome steps used are.
  1. Get the input from the user.
  2. convert the string to the Lower case to avoid any case sensitivity.
  3. apply the logic of the reverse the string.
  4. compare the reversed and original string, if same then the string is a palindrome.
  5. if different string is not a palindrome.
  6. print the result to the user.
without wasting much time lets start.

First of all the C schema.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char const *argv[])
{
return 0;
}

ctype.h is used to get the advantage of the function tolower() later in the program.
Now take the input from the user.

 char name[150],rev[150] ;
int l=0;
printf("Enter a string to check palindrome\n");
// taking input from the user
// string should not have the space in between
scanf("%s",&name);

 Now convert and reverse the string in one goal.

l = strlen(name);

// main logic to reverse the string.
/*
to reverse the string just put the last element to the
first position of the second string.
*/
// with the help of the same we can implement the program of the palindrome.
for (int j = 0; j < l; j++){
// just make sure to make this string a lower case.
name[j] = tolower(name[j]);
rev[l-j-1] = name[j];
}

Now its time to check for the palindromic string.
so to check this use c function to compare the string, strcmp(oneString, otherString).

strcmp() returns zero if both the string are same. otherwise return value other than zero.
So now implement strcmp() in the program.

// printing the string to user output.
if(strcmp(rev,name)==0){
printf("entered string is palindrome\n");
}else{
printf("entered string is not the palindrome\n");
}

That's all for the program of the Palindrome.

Full code in one goal.

#include 
// string header is used to get the advantage of the function of the strlen.
#include
#include

int main(int argc, char const *argv[])
{
char name[150],rev[150] ;
int l=0;
// print something
printf("Enter a string to check palindrome\n");
// taking input from the user
// string should not have the space in between
scanf("%s",&name);
// taking the length of the string entered by user.
l = strlen(name);

// main logic to reverse the string.
/*
to reverse the string just put the last element to the
first position of the second string.
*/
// with the help of the same we can implement the program of the palindrome.
for (int j = 0; j < l; j++){
// just make sure to make this string a lower case.
name[j] = tolower(name[j]);
rev[l-j-1] = name[j];
}
// printing the string to user output.
if(strcmp(rev,name)==0){
printf("entered string is palindrome\n");
}else{
printf("entered string is not the palindrome\n");
}
return 0;
}

sample output of this program is.


palindrome output

If you like this post then check out our other posts.

please share and subscribe to this blog.


Comments

Popular posts from this blog

Second Equation of motion, How to implement using C language

Third Equation of motion, How to implement using C language