- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Longest double string of a Palindrome
Introduction
A contiguous sequence of characters, comprising of upper case, lower case, repetitive or unique alphanumeric characters form a C++ string. Every string is recognized by a unique length parameter, which may be odd or even in nature.
A palindromic string is the same sequence of characters encountered from both the beginning as well as the end. The characters at equivalent positions from start and end do not differ. They may be even length or odd length palindromes. The odd length palindrome has a middle character separating the two equivalent halves.
In this article, we are going to develop a code that takes as input a palindrome string, and computes the double string from it. Let us look at the following example to understand the topic better −
Sample Example
Example 1 :
str : "2tor!@L P0!nt$"
Output : torLPnt !@!$ 20
For instance, all the characters are grouped in the beginning of the output string, which is followed by the occurrence of all the special characters. At last, occur the digits in the output string.
Syntax
str.length()
length()
The length() method in C++ is used to compute the number of characters in the string. It works in linear order and is immutable.
str.substr(st, end)
Substr()
The substr() method is used to access a sub string of the bigger strings from the start to the end-1 position. All the indexes to be accessed should be consecutive and in order.
Parameters −
st − The starting position
end − The ending position to terminate the access of the sub string
str.reverse(str.begin(), str.end())
reverse()
The reverse() method is used to change the order of the characters in the string. The characters are accessed from the end towards the beginning of the string. The length remains the same.
Parameters −
str.begin() − The beginning position of the string
str.end() − The ending position of the string
Algorithm
An input string, str is accepted as input
The length of the string is computed using the length() method, denoted by the variable len.
The half length is also computed and stored in the variable, halflen
The first half of the string is captured using the substring() method from the beginning to the half length of the string, stored in fstr.
If the length of the string is even, the second half of the string begins from the half the length index, denoted by sstr.
Otherwise, the second half of the string begins from the index starting from the half length+1 index
The second half of the obtained sstr is reversed using the inbuilt reverse() method.
Double string is obtained by clubbing both the first and the second halves of the input string
Example
The following C++ code snippet is used to take as input a sample palindromic input string and compute the double string from it −
//including the required libraries #include <bits/stdc++.h> using namespace std; int double_string(string str) { int len = str.length(); int halflen = len/2; string fstr = str.substr(0, halflen); string sstr = ""; if (len % 2 == 0) sstr = str.substr(halflen); else sstr = str.substr(halflen + 1); //reversing the second half of string reverse(sstr.begin(), sstr.end()); // Print the double string cout << "Output Double String : \n" <<fstr << sstr << endl; // Print the length of the double string if (len % 2 == 0) cout <<"Length of Double String : \n" << len << endl; else cout <<"Length of Double String : \n" << len - 1 << endl; } int main() { //declaring an input sample string string str = "buttub"; cout <<"Input String : \n" << str <<"\n"; double_string(str); return 0; }
Output
Input String −: buttub Output Double String − butbut Length of Double String −: 6
Conclusion
Palindromes are an important category of strings which have the same sequence of characters from the begin and the end. Therefore, palindromic strings can easily be split into two parts, where in the first and second parts can at most differ by a single character.