7-26 單詞長度(15 分)
阿新 • • 發佈:2018-07-23
scanf scan -o scanner () see [] eat static
你的程序要讀入一行文本,其中以空格分隔為若幹個單詞,以.
結束。你要輸出每個單詞的長度。這裏的單詞與語言無關,可以包括各種符號,比如it‘s
算一個單詞,長度為4。註意,行中可能出現連續的空格;最後的.
不計算在內。
輸入格式:
輸入在一行中給出一行文本,以.
結束
提示:用scanf("%c",...);
來讀入一個字符,直到讀到.
為止。
輸出格式:
在一行中輸出這行文本對應的單詞的長度,每個長度之間以空格隔開,行末沒有最後的空格。
輸入樣例:
It‘s great to see you here.
輸出樣例:
4 5 2 3 3 4
回頭看看這些基礎題目。。。
import java.util.Scanner;public class Main { static int getlength(String s) { int c = s.length() - 1; if(s.charAt(c) == ‘.‘)return c; return c + 1; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = null; int flag = 0; while(s == null || s.charAt(s.length() - 1) != ‘.‘) { s = in.next(); int c = getlength(s); if(c > 0) { if(flag == 1)System.out.print(‘ ‘); else flag = 1; System.out.print(c); } } } }
7-26 單詞長度(15 分)