
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements