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 .
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
import sysRead the text line by line until the end of the file.
for line in sys.stdin:Iterate over all words in each line of the text.
for word in line.split():For each word in the list, print its length. The word lengths are printed in one line, separated by a space.
print(len(word), end = ' ')