Page 379 - computer science (868) class 11
P. 379
The output of the preceding program is as follows:
Enter a sentence ending with fullstop
How are you? Fine, thank you.
Number of words = 6
Program 10 A class called Longword is defined to print the longest word in a sentence. Assume that
the sentence has a unique longest word and the word separator is space only. The class
description is given below:
Class name : Longword
Data Members
String sen, wrd : To store sentence and longest word
Member Methods
void read() : Accepts sentence
String findlong(String s) : Using recursive technique, finds the longest word in the sentence
void show() : Calls findlong(String) and prints the longest word
static void main() : Creates object and calls other methods
1 import java.util.*;
2 class Longword
3 {
4 String sen, wrd;
5 void read()
6 {
7 Scanner sc = new Scanner(System.in);
8 System.out.println("Enter a sentence ending with fullstop");
9 sen = sc.nextLine();
10 int l = sen.length();
11 sen = " "+sen.substring(0, l-1)+" "; //Sentence starting and ending with space
12 wrd = "";
13 }
14 String findlong(String s)
15 {
16 int p1, p2;
17 String s1;
18 if(s.equals(" "))
19 return(wrd);
20 else // finding the position of space in front and back of each word
377
Recursion 377

