Working model of Lab 3.

This commit is contained in:
Rhinemann 2023-05-07 17:07:56 +03:00
parent 084f131c90
commit 77f96fafa6
1 changed files with 10 additions and 15 deletions

View File

@ -15,8 +15,8 @@ public class lab_3 {
return pTextLower; return pTextLower;
} */ } */
public static void cSubUtil(String string, int leftBoundary, int rightBoundary) public static void cSubUtil(StringBuilder stringbuilder, int leftBoundary, int rightBoundary) {
{ String string = stringbuilder.toString().toLowerCase().replaceAll("[^a-z]","");
// check if the indices lie in the range of string // check if the indices lie in the range of string
// and also if it is palindrome // and also if it is palindrome
while (leftBoundary >= 0 && rightBoundary < string.length() && string.toLowerCase().charAt(leftBoundary) == string.toLowerCase().charAt(rightBoundary)) { while (leftBoundary >= 0 && rightBoundary < string.length() && string.toLowerCase().charAt(leftBoundary) == string.toLowerCase().charAt(rightBoundary)) {
@ -33,7 +33,7 @@ public class lab_3 {
return; return;
} }
public static int longestPalSubstr(String string) public static int longestPalSubstr(StringBuilder string)
{ {
result = ""; result = "";
maxStrLength = 1; maxStrLength = 1;
@ -45,16 +45,15 @@ public class lab_3 {
// check for even length palindromes // check for even length palindromes
cSubUtil(string, i, i + 1); cSubUtil(string, i, i + 1);
} }
System.out.println("Longest palindrome substring is: " + result); System.out.println("Longest palindrome substring is: " + compareStrings(string));
return maxStrLength; return compareStrings(string).length();
} }
public static String compareStrings(StringBuilder builder) { public static String compareStrings(StringBuilder builder) {
System.out.println(result); for (int leftBoundary = 0; leftBoundary <= builder.length(); leftBoundary++) {
for (int leftBoundary = 0; leftBoundary < builder.length(); leftBoundary++) { for (int rightBoundary = builder.length(); leftBoundary >= 0; rightBoundary--) {
for (int rightBoundary = builder.length(); rightBoundary > 0; rightBoundary--) { if (result.equals(builder.substring(leftBoundary, rightBoundary).toString().toLowerCase().replaceAll("[^a-z]",""))) {
if (result.equals(builder.substring(rightBoundary, rightBoundary).toString().toLowerCase().replaceAll("[^a-z]",""))) { return builder.substring(leftBoundary, rightBoundary).toString();
return builder.substring(rightBoundary, rightBoundary).toString();
} }
} }
} }
@ -63,12 +62,8 @@ public class lab_3 {
public static void main(String[] args) { public static void main(String[] args) {
StringBuilder stringToDetect = new StringBuilder("Eva, can I see bees in a cave?"); StringBuilder stringToDetect = new StringBuilder("Eva, can I see bees in a cave?");
String stripped = stringToDetect.toString().toLowerCase().replaceAll("[^a-z]","");
System.out.println("Initial string: " + stringToDetect.toString()); System.out.println("Initial string: " + stringToDetect.toString());
System.out.println("Length is: " + longestPalSubstr(stripped)); System.out.println("Length is: " + longestPalSubstr(stringToDetect));
System.out.println(compareStrings(stringToDetect));
// System.out.println("Eva, can I see bees in a cave?".length());
} }
} }