The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Yes it's possible

Posted by Siddhant Kumar on November 27, 2001 at 1:27 AM

Take the whole string as input first
and than use "space" to remove each letter (better not to remove b'cos it will require lots of variables..) using String class methods u can get "substring" of the entered string where last index can be found out using "space"

best of luck
( better try to understand the problem it's not that much hard, to a trial and error method u can call it as R & D ;)

bye for now

Siddhant Kumar

> I have been asked the following question to solve can anyone help!!

> Assuming that a sentence consists of a sequence of words separated by single spaces and terminated by a full stop, write and implement a Java program that will read in a sentence from the keyboard and will print out each word on a separate line together with the number of letters in each word. For example:

> Mary had a little lamb.

> Mary 4
> had 3
> a 1
> little 6
> lamb 4

> The sentence should be short enough to be entered on one line of input. It should be treated as a sequence of characters, which may be read in one at a time. Each character must be either a letter or a space or a full stop. The program will not start reading the characters until the sentence is terminated and the ENTER key is pressed. The first digit of the number of letters in each word should be vertically aligned and you may assume that no word will be longer than 20 characters.

> I tried to get a solution myself but if you read in it as character you can't read the length of each word that is to be returned. If anybody has a solution please respond as I am in great difficulty here!!

> Thanks
> A distressed programming student.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us