Check if the String formed by the first and last X characters of a String is a Palindrome


In this tutorial, we will explore a problem statement that involves checking whether a string, formed by combining the first X characters and the last X characters of a given string, is a palindrome or not. A palindrome refers to a sequence of characters that remain unchanged when reading both forwards and backwards.

The task is to determine if the resulting combined string, derived from the specified characters at the beginning and end of the original string, exhibits palindrome properties.

To accomplish this, we will delve into an effective solution using the C++ programming language. By breaking down the problem into smaller steps and employing string manipulation techniques, we will develop a program that can verify whether the given string satisfies the palindrome condition. Through the implementation of this solution, we aim to provide a clear understanding of the problem and present a practical approach to solving it using C++.

Problem Statement

Given a string and an integer value X, the task is to check if the string formed by the first and last X characters of the given string is a palindrome or not. Return true if it is a palindrome, otherwise, return false. Let’s understand this problem statement with examples.

Sample Example 1

Input

String: "helloolleh"; X: 5

Output

The formed string is a palindrome.

Explanation

In this example, the input string is "helloolleh" and the value of X is 5. The program extracts the first 5 characters ("hello") and the last 5 characters ("olleh") from the input string and concatenates them to form the string "helloolleh". Since this formed string is a palindrome, the program returns true.

Sample Example 2

Input

String: "programming"; X: 4

Output

The formed string is not a palindrome.

Explanation

In this example, the input string is "programming" and the value of X is 4. The program extracts the first 4 characters ("prog") and the last 4 characters ("ming") from the input string and concatenates them to form the string "progmimg". Since this formed string is not a palindrome (it is "gimmorp"), the program returns false.

In both examples, the program checks if the string formed by the first and last X characters of the given string is a palindrome or not, providing the output accordingly.

Algorithm

1. Define a helper function ‘isPalindrome’ that takes a string as input and checks if it is a palindrome. Iterate over the string from both ends, comparing the characters. If a mismatch is found, return false. If the iteration completes without any mismatches, return true.

2. Define the main function ‘checkPalindromeFormation’ that takes the input string and integer X as parameters.

3. Within the ‘checkPalindromeFormation’ function, first check if X is a valid value (greater than 0 and not exceeding the length of the input string). If it's not valid, return false.

4. Use the ‘substr’ function to extract the first X characters from the input string and assign it to the ‘firstX’ variable.

5. Similarly, extract the last X characters from the input string and assign it to the ‘lastX’ variable.

6. Concatenate ‘firstX’ and ‘lastX’ to form the ‘formedString’.

7. Call the isPalindrome helper function, passing the ‘formedString’ as the argument, and store the result in a boolean variable ‘isPalindromeFormed’.

8. Return the value of ‘isPalindromeFormed’ from the ‘checkPalindromeFormation’ function.

9. In the main function, provide an input string and an integer value X for testing.

10. Call the ‘checkPalindromeFormation’ function with the input string and X as arguments, and store the result in a boolean variable ‘isPalindromeFormed’.

11. Print the input string and X.

12. Print the message "Checking if the string formed by the first and last X characters is a palindrome..."

13. Use an if-else statement to check the value of ‘isPalindromeFormed’. If it's true, print "The formed string is a palindrome." Otherwise, print "The formed string is not a palindrome."

After understanding the algorithm let’s implement this algorithm with the help of an example using C++.

Example

Implementation of the above algorithm using C++

The below C++ program checks if the string formed by the first and last X characters of a given string is a palindrome. It does this by extracting the substrings, concatenating them, and then using a helper function to check for palindrome properties.

#include <iostream>
#include <string>
bool isPalindrome(const std::string& str) {
   int start = 0;
   int end = str.length() - 1;
   while (start < end) {
      if (str[start] != str[end]) {
         return false;
      }
      start++;
      end--;
   }
   return true;
}
bool checkPalindromeFormation(const std::string& inputStr, int x) {
   if (x <= 0 || x > inputStr.length()) {
      return false;
   }
   std::string firstX = inputStr.substr(0, x);
   std::string lastX = inputStr.substr(inputStr.length() - x, x);
   std::string formedString = firstX + lastX;
   return isPalindrome(formedString);
}
int main() {
   std::string inputStr = "helloolleh";
   int x = 5;
   std::cout << "Input string: " << inputStr << std::endl;
   std::cout << "Checking if the string formed by the first and last " << x << " characters is a palindrome..." << std::endl;
   bool isPalindromeFormed = checkPalindromeFormation(inputStr, x);
   if (isPalindromeFormed) {
      std::cout << "The formed string is a palindrome." << std::endl;
   } else {
      std::cout << "The formed string is not a palindrome." << std::endl;
   }
   return 0;
}

Output

Input string: helloolleh
Checking if the string formed by the first and last 5 characters is a 
palindrome...
The formed string is a palindrome.

Conclusion

To sum up, we have addressed the problem of checking whether a string, formed by combining the first and last X characters of a given string, is a palindrome. By utilizing the power of the C++ programming language, we have developed a solution that efficiently handles the task. Through the implementation of string manipulation techniques and a simple palindrome-checking algorithm, we have provided a reliable and accurate means of determining the palindrome property of the resulting string. This tutorial serves as a comprehensive guide, offering insights into the problem statement, the step-by-step approach to solving it, and the underlying C++ implementation. By understanding and applying the concepts presented here, readers can effectively handle similar challenges involving string manipulations and palindrome validations in their own projects.

Updated on: 08-Sep-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements