ADVERTISEMENT
ADVERTISEMENT

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.

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.