find palindrome substring in string in java code example
Example 1: Java program to check palindrome string using recursion
import java.util.Scanner;
public class RecursivePalindromeJava
{
public static boolean checkPalindrome(String str)
{
if(str.length() == 0 || str.length() == 1)
return true;
if(str.charAt(0) == str.charAt(str.length() - 1))
return checkPalindrome(str.substring(1, str.length() - 1));
return false;
}
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a string : ");
String strInput = sc.nextLine();
if(checkPalindrome(strInput))
{
System.out.println(strInput + " is palindrome");
}
else
{
System.out.println(strInput + " not a palindrome");
}
sc.close();
}
}
Example 2: Java program to check whether string is palindrome using library methods
public class StringPalindromeJava
{
public static void isPalindrome(String str)
{
String strReverse = new StringBuffer(str).reverse().toString();
if(str.equals(strReverse))
{
System.out.println(str + " is palindrome string.");
}
else
{
System.out.println(str + " is not palindrome string.");
}
}
public static void main(String[] args)
{
isPalindrome("eye");
isPalindrome("rotator");
}
}
Example 3: find a substring in a string java
package hello;
public class SubStringProblem {
public static void main(String[] args) {
System.out
.println("Checking if one String contains another String using indexOf() in Java");
String input = "Java is the best programming language";
boolean isPresent = input.indexOf("Java") != -1 ? true : false;
if (isPresent) {
System.out.println("input string: " + input);
System.out.println("search string: " + "Java");
System.out.println("does String contains substring? " + "YES");
}
System.out.println("Doing search with different case");
isPresent = input.indexOf("java") != -1 ? true : false;
System.out.println("isPresent: " + isPresent);
System.out
.println("Checking if one String contains another String using contains() in Java");
input = "C++ is predecessor of Java";
boolean isFound = input.contains("Java");
if (isFound) {
System.out.println("input string: " + input);
System.out.println("search string: " + "Java");
System.out.println("does substring is found inside String? " + "YES");
}
System.out.println("Searching with different case");
isFound = input.contains("java");
System.out.println("isFound: " + isFound);
}
}
Output
Checking if one String contains another String using indexOf() in Java
input string: Java is the best programming language
search string: Java
does String contain substring? YES
Doing search with different case
isPresent: false
Checking if one String contains another String using contains() in Java
input string: C++ is the predecessor of Java
search string: Java
does substring is found inside String? YES
Searching for different case
isFound: false
Example 4: find all the palindrome substring in a given string
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
void expand(string str, int low, int high, auto &set)
{
while (low >= 0 && high < str.length()
&& str[low] == str[high])
{
set.insert(str.substr(low, high - low + 1));
low--, high++;
}
}
void allPalindromicSubstrings(string str)
{
unordered_set<string> set;
for (int i = 0; i < str.length(); i++)
{
expand(str, i, i, set);
expand(str, i, i + 1, set);
}
for (auto i : set)
cout << i << " ";
}
int main()
{
string str = "google";
allPalindromicSubstrings(str);
return 0;
}