
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Check if a string is palindrome in C using pointers
Suppose we have a string s. We have to check whether the given string is a palindrome or not. We have to solve this problem using pointers in C.
So, if the input is like s = "racecar", then the output will be True.
To solve this, we will follow these steps −
- length := size of string
- forward := pointing to the first character of string
- reverse := pointing to the last character of string
- while position of reverse >= position of forward, do
- if character pointed by reverse is same as character pointed by forward, then
- increase forward and decrease reverse by 1
- otherwise
- come out from loop
- if character pointed by reverse is same as character pointed by forward, then
- if position of forward >= position of reverse, then
- return True
- return False
Let us see the following implementation to get better understanding −
Example
#include <stdio.h> #include <string.h> int solve(char *string){ int length; char *forward, *reverse; length = strlen(string); forward = string; reverse = forward + length - 1; for (forward = string; reverse >= forward;) { if (*reverse == *forward) { reverse--; forward++; } else break; } if (forward > reverse) return 1; else return 0; } int main(){ char string[] = "racecar"; printf("%d", solve(string)); }
Input
"racecar"
Output
1
- Related Articles
- How to check if String is Palindrome using C#?
- Recursive function to check if a string is palindrome in C++
- C Program to Check if a Given String is a Palindrome?
- Check if a given string is a rotation of a palindrome in C++
- C# program to check if a string is palindrome or not
- Check if a number is Palindrome in C++
- JavaScript - Find if string is a palindrome (Check for punctuation)
- Python program to check if a string is palindrome or not
- Python program to check if a given string is number Palindrome
- Python Program to Check String is Palindrome using Stack
- Check if any anagram of a string is palindrome or not in Python
- Check if the value entered is palindrome or not using C language
- TCP Client-Server Program to Check if a Given String is a Palindrome
- How to find if a string is a palindrome using Java?
- How to Check Whether a String is Palindrome or Not using Python?

Advertisements