C++ Examples

WAP in C++ Program to Check if a Given String is a Palindrome or not

The Program in C++ Program to Check if a Given String is a Palindrome or not is given below:

#include <iostream>
#include <string>

using namespace std;

bool isPalindrome(string s) {
    int n = s.length();
    for (int i = 0; i < n / 2; i++) {
        if (s[i] != s[n - i - 1]) {
            return false;
        }
    }
    return true;
}

int main() {
    string s;
    cout << "Hello Codeauri Family, Enter a string here to check whether the given string is palindrome ot not!: ";
    cin >> s;
    if (isPalindrome(s)) {
        cout << s << " is a palindrome." << endl;
    } else {
        cout << s << " is not a palindrome." << endl;
    }
    return 0;
}

Output:

Hello Codeauri Family, Enter a string here to check whether the given string is palindrome ot not!: pop
pop is a palindrome.

Pro-Tips💡

isPalindrome takes a string as an argument and returns a boolean value indicating whether the string is a palindrome or not.

It does this by comparing the first and the last character of the string, then the second and the second to last character, and so on.

If any of the characters do not match, the function returns false. If all the characters match, the function returns true.

main is the entry point of the program. It prompts the user to enter a string, then calls the isPalindrome function and prints the result.

If the result of the isPalindrome function is true, it means that the string is a palindrome, and the program prints “is a palindrome”.

If the result is false, it means that the string is not a palindrome, and the program prints “is not a palindrome”.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X