
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
Check If a String Contains a Palindromic Sub-String of Even Length in C++
Suppose, we are given a string that contains only lowercase letters. Our task is to find if there exists a substring in the given string that is a palindrome and is of even length. If found, we return 1 otherwise 0.
So, if the input is like "afternoon", then the output will be true.
To solve this, we will follow these steps −
- for initialize x := 0, when x < length of string - 1, increase x by 1, do −
- if string[x] is same as string[x + 1], then:
- return true
- if string[x] is same as string[x + 1], then:
- return false
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(string string) { for (int x = 0; x < string.length() - 1; x++) { if (string[x] == string[x + 1]) return true; } return false; } int main() { cout<<solve("afternoon") <<endl; }
Input
"afternoon"
Output
1
Advertisements