Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,037 questions

40,846 answers

573 users

How to get all palindrome substrings in a string with C++

1 Answer

0 votes
#include <bits/stdc++.h> 

using namespace std;

bool is_palindrome(string s, int i, int j) {
    string rev = s.substr(i, j - i);
    reverse(rev.begin(), rev.end()); 

    string tmp_s = s.substr(i, j - i);
     
    bool b = false;
    if (tmp_s == rev && tmp_s.length() >= 2) {
        b = true;
    }

    return b;
}
 
int main() {
    string s = "abaab";
      
    for (int i = 0; i < s.length() - 1; i++) { 
        for (int j = i + 1; j <= s.length(); j++) { 
             if (is_palindrome(s, i, j)) {
                cout << s.substr(i, j - i) << endl;
             }
        }
    }
    
    return 0;
}
 
  
  
/*
run:
  
aba
baab
aa

*/

 





answered Oct 22, 2019 by avibootz

Related questions

1 answer 103 views
1 answer 82 views
1 answer 83 views
1 answer 107 views
...