Computer Applications
Define a class StringMinMax to find the smallest and the largest word present in the string.
E.g.
Input:
Hello this is wow world
Output:
Smallest word: is
Largest word: Hello
Java
Java String Handling
9 Likes
Answer
import java.util.Scanner;
public class StringMinMax
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
str += " ";
String word = "", lWord = "", sWord = str;
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
if (word.length() > lWord.length())
lWord = word;
else if (word.length() < sWord.length())
sWord = word;
word = "";
}
else {
word += ch;
}
}
System.out.println("Smallest word: " + sWord);
System.out.println("Longest word: " + lWord);
}
}
Output

Answered By
3 Likes
Related Questions
Write a program to accept 10 different decimal numbers (double data type) in a Single Dimensional Array (say, A). Truncate the fractional part of each number of the array A and store their integer part in another array (say, B).
Write a program to print the following patterns.
(i)
5 4 5 3 4 5 2 3 4 5 1 2 3 4 5
(ii)
J I H G F E D C B A
A class Employee contains the following member:
Class name : Employee
Data members/Instance variables
String ename : To store the name of employee
int ecode : To store the employee code
double basicpay : To store the basic pay of employeeMember functions/Methods
Employee( - - - - ) : An argumented constructor to assign name, employee code and basic salary to data members/instance variables
double salCalc( ) : To compute and return the total salary of an employee
void display( ) : To display ename, ecode, basicpay and calculated salaryThe salary is calculated according to the following rules :
Salary = Basic pay + HRA + DA + TA
where, HRA = 20% of basic pay
DA = 25% of basic pay
TA = 10% of basic payif the ecode <= 100, then a special allowance (20% of salary) will be added and the maximum amount for special allowance will be 2500.
if the ecode > 100 then the special allowance will be 1000.
Hence, the total salary for the employee will calculated as :
Total Salary = Salary + Special Allowance
Specify the class Employee giving the details of the constructor, double salCalc() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.
Write a program to accept the year of graduation from school as an integer value from the user. Using the binary search technique on the sorted array of integers given below, output the message "Record exists" if the value input is located in the array. If not, output the message "Record does not exist".
Sample Input:n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] 1982 1987 1993 1996 1999 2003 2006 2007 2009 2010