Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » C Programming » Programs » Strings » C program to find a string is palindrome or not
Suryateja Pericherla Categories: Strings. No Comments on C program to find a string is palindrome or not
5
(1)

In this article we will try to implement a C program to find a string is palindrome or not. A C program is provided below to check if the given string is a palindrome or not.

 

The program is:

/*
 * C program to implement the following string functions using user defined functions:
 * i. Length of a string
 * ii. Find whether the given string is a palindrome or not
 * Author: P.S.SuryaTeja
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

void length(char *str)
{
	int count = 0;
	while(*str != '\0')
	{
		count++;
		str++;
	}
	printf("\nLength of the string is: %d", count);
}

void ispalindrome(char *str)
{
	char *revstr, *startstr;
	int stringlength = strlen(str);
	int i, flag = 0;
	startstr = str;
	for(i = 0; i < stringlength - 1; i++)
		str++;
	revstr = str;
	while(startstr < revstr)
	{
		if(*startstr != *revstr)
			flag = 1;
		startstr++;
		revstr--;
	}
	if(flag == 0)
		printf("\nEntered string is a palindrome");
	else
		printf("\nEntered string is not a palindrome");
}

int main(int argc, char **argv)
{
	char str[20];
	printf("Enter a string: ");
	gets(str);
	printf("\n");
	length(str);
	printf("\n");
	ispalindrome(str);
    getch();
    return 0;
}

 

Input and output for the above program is as follows:

Enter a string: madam

Length of the string is: 5

Entered string is a palindrome

 

How useful was this post?

Click on a star to rate it!


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory