Skip to content

Commit 485ecb2

Browse files
committed
Solution to print if a String is a Palindrome or not
1 parent a144797 commit 485ecb2

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎src/Strings/CompareTheStrings.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Strings;
2+
3+
public class CompareTheStrings {
4+
public static void main(String[] args) {
5+
6+
int returnValue = solve("interview","bit");
7+
System.out.println(returnValue);
8+
}
9+
10+
static int solve(String A, String B) {
11+
12+
int length1 = A.length();
13+
int length2 = B.length();
14+
15+
int finalLength = 0;
16+
17+
if (length1 > length2){
18+
finalLength = length2;
19+
}else{
20+
finalLength = length1;
21+
}
22+
23+
int string1Value = 0;
24+
int string2Value = 0;
25+
26+
boolean isStringEqual = false;
27+
28+
for(int i = 0; i <finalLength; i++){
29+
int value1 = A.charAt(i);
30+
int value2 = B.charAt(i);
31+
32+
if (value1 < value2){
33+
return -1;
34+
}else if(value1 > value2){
35+
return 1;
36+
}
37+
}
38+
39+
return 0;
40+
41+
// System.out.println("String1Value is " + string1Value);
42+
// System.out.println("String2Value is " + string2Value);
43+
}
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Strings;
2+
3+
public class isPalindromeString {
4+
public static void main(String[] args) {
5+
String string = "abcba";
6+
7+
int value = isPalindrome(string);
8+
System.out.println("Is the String Palindrome: " + value);
9+
}
10+
11+
static int isPalindrome(String A){
12+
int lengthOfString = A.length();
13+
14+
for (int i = 0; i < lengthOfString; i++){
15+
16+
int currentIndex = i;
17+
18+
int comparisonIndex = lengthOfString-i-1;
19+
20+
System.out.println(currentIndex + " " + comparisonIndex);
21+
22+
if (currentIndex > comparisonIndex){
23+
break;
24+
}
25+
26+
if (A.charAt(currentIndex) != A.charAt(comparisonIndex)){
27+
return 0;
28+
}
29+
}
30+
31+
return 1;
32+
}
33+
}

0 commit comments

Comments
 (0)