logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Check Whether String is Palindrome

Posted on August 5, 2013 by Manish

This is a C++ Program to Find if a String is Palindrome.

Problem Description

The program checks if a string is a palindrome or not. A palindrome is a word or a string that reads the same backward and forward.

Problem Solution

1. The program takes a string and stores it.
2. The string is copied into another string from backwards.
3. If both the strings are equal, then the entered string is a palindrome.
4. Else it is not.
5. The result is printed.
6. Exit.

advertisement
C++ Program/Source code

Here is the source code of C++ Program to Find if a String is Palindrome. The program output is shown below.

  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     char str1[20], str2[20];
  7.     int i, j, len = 0, flag = 0;
  8.     cout << "Enter the string : ";
  9.     gets(str1);
  10.     len = strlen(str1) - 1;
  11.     for (i = len, j = 0; i >= 0 ; i--, j++)
  12.         str2[j] = str1[i];
  13.     if (strcmp(str1, str2))
  14.         flag = 1;
  15.     if (flag == 1)
  16.         cout << str1 << " is not a palindrome";
  17.     else
  18.         cout << str1 << " is a palindrome";
  19.     return 0;
  20. }
Program Explanation

1. The user is asked to enter a string and it is stored in the character variable ‘str1’.
2. The length of str1 is stored in ‘len’ using the string function strlen().
3. Using a for loop, str1 is copied into another variable ‘str2’ from backwards.
4. Both the strings str1 and str2 are compared using string function strcmp().
5. A temporary variable flag is used.
6. If str1 is equal to str2, then the entered string is a palindrome, else not.
7. The result is then printed.

advertisement
Runtime Test Cases
Case 1 :
Enter the string : nun
nun is a palindrome
 
Case 2 :
Enter the string : fast                                                                                                        
fast is not a palindrome
 
Case 3 :
Enter the string : 121                                                                                                         
121 is a palindrome

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

« Prev Page - C++ Program to Implement Cartesian Tree
» Next Page - C++ Program to Compute the Area of Circle

« Software Design Questions and Answers – Layered Architectures
Software Design Questions and Answers – Collection Iterator Pattern »

Deep Dive @ Sanfoundry:

  1. Python Programming Examples
  2. C# Programming Examples on Strings
  3. C Programming Examples
  4. Simple C Programs
  5. C Programming Examples using Recursion
  6. C Programming Examples on Bitwise Operations
  7. C Programming Examples on Strings
  8. C Programming Examples on Stacks & Queues
  9. C++ Programming Examples on Set & String Problems & Algorithms
  10. C Programming Examples on Set & String Problems & Algorithms
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.