-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07 Oct 2022.java
More file actions
74 lines (56 loc) · 1.62 KB
/
Copy path07 Oct 2022.java
File metadata and controls
74 lines (56 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Problem Statement : 07 Oct 2022
// Accept a string from user and Return LARGEST PALINDROME word from it
// Input : str1 = "This is a racecar"
// Output : "racecar"
// Input : str1 = "rotator did mistakes"
// Output : "rotator"
// Input : str1 = "christmas eve is better than noon"
// Output : "noon"
// Solution:-
import java.util.Scanner;
public class Main
{
static boolean checkPalin(String word)
{
int n = word.length();
word = word.toLowerCase();
for (int i = 0; i < n; i++, n--)
if (word.charAt(i) !=
word.charAt(n - 1))
return false;
return true;
}
static String longestPalin(String str)
{
str = str + " ";
String longestword = "", word = "";
int length, length1 = 0;
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (ch != ' ')
word = word + ch;
else {
length = word.length();
if (checkPalin(word) &&
length > length1)
{
length1 = length;
longestword = word;
}
word = "";
}
}
return longestword;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if (longestPalin(s) == "")
System.out.println("No Palindrome"
+ " Word");
else
System.out.println(longestPalin(s));
}
}