C++ - Check String is Palindrome or Not without using Library Function using C++ Program.


IncludeHelp 14 August 2016

In this code snippet we will learn how to check a given string is Palindrome or not in c++?

In this program we will not use any library function like strcpy, strrev etc except of strlen function.

C++ Code Snippet - Check String is Palindrome or Not w/o using Library Function

/*C++ - Check String is Palindrome or Not without 
using Library Function using C++ Program.*/

#include <iostream>
#include <string.h>

using namespace std;

int main(){

	char text[100];
	char temp;
	int isPalindrome = 1;

	cout << "Enter a word: ";
	cin >> text;

	int length = strlen(text); //length of the text

	for(int i=0; i<length; i++){
		
		if(text[0+i] == text[(length-1)-i]){
			//swap characters
			temp = text[0+i];
			text[0+i] = text[(length-1)-i];
			text[(length-1)-i] = temp;
		}
		else{
			isPalindrome = 0; //string is not a palindrome
			break;
		}
	}

	if(isPalindrome == 0)
		cout << text << " is not a Palindrome String" << endl;
	if(isPalindrome == 1)
		cout << text << " is a Palindrome String." << endl;

	return 0;
}

    First Run:
    Enter a word: includehelp
    includehelp is not a Palindrome String

    Second Run:
    Enter a word: abcdedcba
    abcdedcba is a Palindrome String.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.