-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path39.1 Largest word in string.java
67 lines (53 loc) · 1.74 KB
/
39.1 Largest word in string.java
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
/*
Rohit is pursuing his B.Tech degree in CSE. His java teacher given the assignment to write java code to accept a string/sentence in upper case if the string/sentence in not upper case than convert it in uppercase and Display the longest word and the length of the longest word present in the string. If the string contains number or string is null then display the massage “Invalid input”.
Sample 1:
Enter string: Lovely Professional University
Longest word : PROFESSIONAL
Sample 2:
Enter string: 123 cse 45 lpu 56789
Invalid input
Input Format
First line read the input as String
Constraints
numbers of word in string > 1
Output Format
Display longest string in Uppercase
Sample Input 0
Lovely Professional University
Sample Output 0
PROFESSIONAL
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
String input = sc.nextLine().toUpperCase();
char []array = input.toCharArray();
for(char c : array)
{
if(Character.isDigit(c))
{
System.out.println("Invalid input");
System.exit(0);
}
}
String []str = input.split(" ");
if(str.length <= 1)
{
System.out.print("Invalid input");
System.exit(0);
}
int max = str[0].length();
int maxi = 0;
for(int i=1; i<str.length;i++)
{
if(str[i].length() > max)
{
max = str[i].length();maxi=i;
}
}
System.out.print(str[maxi]);
}
}