The six-week programming problem
In accordance with the terms of academic integrity, I guarantee that this work is done independently.
1
Word Length (4 points)
Topic content:
Your program reads a line of text, separated by a space into several words, to '. ' End. You want to output the length of each word in this line of text. The words here are not language-related, and can include various symbols, such as "it's" to count a word, length 4. Note that there may be contiguous spaces in the row.
Input format:
Enter a line of text in a row to '. ' End, the ending period cannot be counted within the length of the last word.
Output format:
Prints the length of the word for the line text in a row, separated by a space between each length, with no last space at the end of the line.
Input Sample:
It's great to see your here.
Sample output:
4 5 2 3 3 4
Time limit: 500ms memory limit: 32000kb
Import Java.util.Scanner; public class Hello{public static void Main (string[] args) {//TODO auto-generated method Stubscanner in=new Scanner (System . in); String S=in.next (); Char ch= '. '; while (S.length () >0)//is used to read each word separately,//The flaw is if the word entered in the middle of the sentence is "." is considered to be the end of the statement, which is not considered in this context. {if (S.charat (S.length ()-1) ==ch) {System.out.print ((s.substring (0, S.length ()-1)). Length ()); break;} Else{system.out.print (s.length () + ""); S=in.next ();}}}
Method two (more standard):
Import Java.util.Scanner; public class Hello{public static void Main (string[] args) {//TODO auto-generated method Stubscanner in=new Scanner (System . in); String S=in.nextline (); Char ch= '. '; String[] Word=s.split ("\\s{1,}");//Regular expression, one or more spaces for (int i=0;i<word.length-1;i++) {System.out.print (Word[i]. Trim (). Length () + "");//with regular expressions, trim () may not}string Wend=word[word.length-1].trim (); if (Wend.charat (Wend.length ()-1) ==ch) {System.out.print (wend.substring (0, Wend.length ()-1). Length ());}}}
Introduction to Programming-java language Sixth week programming question 1 words length (4 points)