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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,987 questions

51,931 answers

573 users

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

1 Answer

0 votes
using System;
 
class Program
{
    public static string reverse_string(string s) {
            char[] arr = s.ToCharArray();
  
            Array.Reverse(arr);
  
            return new string(arr);
    }
    static bool is_palindrome(string s, int i, int j) {
        string  rev = s.Substring(i, j - i); 
        rev = reverse_string(rev);
           
        string tmp_s = s.Substring(i, j - i); 
        bool b = false;
        if (tmp_s == rev && tmp_s.Length >= 2) {
            b = true;
        }
        
        return b;
    }
    static void Main() {
        string s = "abaab";
          
        for (int i = 0; i < s.Length; i++) { 
            for (int j = i + 1; j <= s.Length; j++) { 
                if (is_palindrome(s, i, j)) {
                     Console.WriteLine(s.Substring(i, j - i));
                }
            }
        }
    }
}
 
 
 
/*
run:
 
aba
baab
aa
 
*/

 



answered Oct 23, 2019 by avibootz
edited Oct 24, 2019 by avibootz

Related questions

1 answer 142 views
1 answer 140 views
1 answer 177 views
1 answer 153 views
1 answer 143 views
1 answer 162 views
1 answer 158 views
...