
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
A contiguous sequence of characters, consisting of uppercase, lowercase, repetitive, or unique alphanumeric characters, forms a C++ string. Each string has a unique length, which may be either odd or even in nature.
Longest Double String of a Palindrome
A Palindromic string is a sequence of characters that reads the same from both the beginning and the end. In other words, characters at equivalent positions from the start and end have no difference.
These palindromes can either have an even length or an odd length. The odd-length palindrome features a middle character separating the two equivalent halves.
In this article, we are going to develop code that takes a palindrome string as input and computes the "double string" from it. Let us look at the following example to understand the topic better ?
Example Scenario
Following is an input-output scenario for the requirement -
Input: 2tor!@L P0!nt$" Output: torLPnt !@!$ 20
In this example, all the characters are grouped at the beginning of the output string, which is followed by the occurrence of all the special characters. At last, the digits occur in the output string.
String Functions Used
We will use some String functions in our solution, so below are some methods we are going to use to solve the Longest double string of a Palindrome problem.
-
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.
string str = "hello"; int len = str.length();
-
substr(start, length): The substr() method is used to access a substring of the bigger string from the start to the end-1 position. All the indices to be accessed should be consecutive and in order.
string str = "example"; string subStr = str.substr(0, 4); // "exam"
-
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.
string str = "reverse"; reverse(str.begin(), str.end()); // "esrever"
Algorithm to Create a Double String
Following are the steps to Create a Double String in C++ -
- Accept a palindrome string str as input.
- Compute the length of the string using str.length(), and store it in len.
- Compute half of the string's length and store it in halflen.
- Extract the first half of the string using substr() and store it in fstr.
- If the string's length is even, extract the second half starting from the middle of the string. Otherwise, extract the second half starting from the character after the middle.
- Reverse the second half of the string using reverse().
- Concatenate the first half and the reversed second half to create the "double string".
Example
Following is an example -
#include <bits/stdc++.h> using namespace std; void double_string(string str) { int len = str.length(); int halflen = len / 2; // Extract the first half string fstr = str.substr(0, halflen); string sstr; // Extract the second half if (len % 2 == 0) sstr = str.substr(halflen); else sstr = str.substr(halflen + 1); // Reverse the second half reverse(sstr.begin(), sstr.end()); // Concatenate both halves cout << "Output Double String: " << fstr << sstr << endl; // Print the length of the double string if (len % 2 == 0) cout << "Length of Double String: " << len << endl; else cout << "Length of Double String: " << len - 1 << endl; } int main() { string str = "buttub"; cout << "Input String: " << str << endl; double_string(str); return 0; }
Following is the output of the above program -
Input String: buttub
Output Double String: butbut
Length of Double String: 6