Given a text containing a sequence of words. Find the length of each
word.
Input. The text contains a sequence of words. The length of each word is no
more than 20 characters.
Output. Print the length of each word on a single line.
Sample input 1 |
Sample output 1 |
Programming Principles 1 |
11 10 1 |
|
|
Sample input 2 |
Sample output 2 |
I like C very much |
1 4 1 4 4 |
strings
Read the text
word by word using the scanf function with the “%s” format. Determine
the length of the word using the strlen function.
When using the C++
language, read words into string type variables using the cin
function. Use the size method to find the length of the word.
Algorithm
implementation
Read the word into array s.
char s[100];
Read the
text word by word until the end of the file. For each word print its length.
while(scanf("%s",s)
== 1)
printf("%d ",strlen(s));
Algorithm
implementation – C++
Read the
text word by word until the end of the file. For each word print its length.
while (cin >> s)
cout << s.size() << " ";
cout << endl;
Java implementation
import
java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new
Scanner(System.in);
while(con.hasNext())
{
String s = con.next();
System.out.print(s.length()
+ " ");
}
con.close();
}
}
Python implementation
Read the text line by line until
the end of the file.
import sys
for x in sys.stdin:
x = x.split()
The variable x contains a
list of words from one
line.
for i in x:
For each word i from the list x print its
length. The lengths of the words are printed on one line, separated by a single space.
print(len(i), end = ' ')