Length and Palindromicity of a String using Pointers - IndianTechnoEra
Latest update Android YouTube

Length and Palindromicity of a String using Pointers

 

C++ Program to Determine the Length and Palindromicity of a String using Pointers


Problem:

Write a C++ Program which accepts a string from the user and determines its length and also determines if it is a palindrome. Use pointer notation to access the elements of the string.

Step 1: Declare a character type pointer variable and integers, iCount and iLen.

Step 2: Read value for string from keyboard

Step 3: Find the length of the string using strlen() as well as include the corresponding header file <string.h>

Step 4: Iterate the loop for len/2 times to compare the characters by extracting a character from left and at the same time a character from right

Step 5: Display whether the given string is a palindrome or not


Solution

In this blog, we will create a C++ program that accepts a string from the user, determines its length, and checks whether it is a palindrome or not. To access the elements of the string, we will use pointer notation. Palindromes are strings that read the same forwards and backward.

Here's a step-by-step guide to create this program:

Step 1: Declare Variables

#include<iostream>

#include<string.h>

using namespace std;


int main() {

    char* str;        // Declare a character type pointer variable

    int iCount = 0;   // Initialize a variable to count the length of the string

    int iLen;         // Declare a variable to store the length of the string


Step 2: Read String from User

    cout << "Enter a string: ";

    cin >> str;      // Read the string from the user


Step 3: Calculate String Length

    iLen = strlen(str); // Find the length of the string using strlen() function


Step 4: Check for Palindrome

We will use a loop to compare characters from both ends of the string to determine if it's a palindrome.

    bool isPalindrome = true; // Assume it's a palindrome by default

    for (int i = 0; i < iLen / 2; i++) {

        if (str[i] != str[iLen - 1 - i]) {

            isPalindrome = false;

            break;

        }

    }


Step 5: Display the Result

    if (isPalindrome) {

        cout << "The given string is a palindrome." << endl;

    } else {

        cout << "The given string is not a palindrome." << endl;

    }


    cout << "Length of the string is: " << iLen << endl;


    return 0;

}

Now that we've completed the code, here's how it works:


We declare a character pointer (char*) to hold the user's string, along with two integer variables, iCount and iLen.


The user is prompted to enter a string, which is then stored in the str variable.


We calculate the length of the string using the strlen() function and store it in the iLen variable.


To check if the string is a palindrome, we use a loop to compare characters from the beginning and end of the string. If we find a mismatch, we set the isPalindrome flag to false and break out of the loop.


Finally, we display whether the string is a palindrome and its length.


Make sure to execute this code in your C++ compiler to test it with different inputs and verify the results. Palindromes are always fascinating to explore, and this program helps you identify them with ease.

Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.