当前位置:网站首页>OJ daily practice - word length

OJ daily practice - word length

2022-06-22 23:33:00 KJ. JK

Problem description :

Enter a line of word sequence , Between adjacent words by 1 Space or spaces , Please calculate the length of each word accordingly .

Be careful , If there's punctuation ( Such as hyphen , comma ), Punctuation is counted as part of the word associated with it . There is no open space between strings , All count as words .

Input
A line of words , least 1 Word , most 300 Word , Use at least... Between words 1 Space space . The total length of the word sequence does not exceed 1000.

Output
Output the length of the corresponding word in turn , They are separated by commas .

Examples

Input
She was born in 1990-01-02 and from Beijing city.

Output
3,3,4,2,10,3,4,7,5


Java Code :

import java.util.Scanner;
public class Main {
    
public static void main(String[] args) {
    
	Scanner in=new Scanner(System.in);
	int sum=0;
	String a=in.nextLine();
	String b=a.replaceAll(" +", " ");
	char c[]=b.toCharArray();
	for(int i=0;i<c.length;i++) {
    
		if(c[i]==' ') {
    
				System.out.print(sum+",");
			sum=0;
		}
		else {
    
			sum++;
			if(i==c.length-1) {
    
				System.out.println(sum);
			}
		}
	}
}
}

author :KJ.JK
If the article is helpful to you , Welcome to praise or star, Your support is the greatest encouragement to the author , The shortcomings can be corrected in the comments section , Communication and learning

原网站

版权声明
本文为[KJ. JK]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222059454585.html