请教一个面试问题

yyjl1983 2008-09-09 03:05:50
请你用java写一个方法,计算字符串“KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world”的单词个数。
...全文
111 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobor_2008 2008-09-09
  • 打赏
  • 举报
回复
public static void main(String[] args) {
String date = (new Date(System.currentTimeMillis())).toString();
String abc="“KWA,fd,fsd,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world”";
String []abcd=abc.split(" |,|,");
int i=0;
for(String s:abcd){
System.out.println("第["+(++i)+"]个单词是:"+s);
System.out.println(++i);
}
System.out.println("总共:"+i+"个单词.");
}

=================================================
这段代码是仅针对你这段作的统计单词的个数.
yyjl1983 2008-09-09
  • 打赏
  • 举报
回复
谢谢大家 正则表达式应该是最好的答案了
yeruping 2008-09-09
  • 打赏
  • 举报
回复

String s = "KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";
String[] str=(s.split("[\\,| ]"));
System.out.println(str.length);

主要是半角还是全角的问题 ....
justinavril 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lizhi1026 的回复:]
引用 1 楼 justinavril 的回复:
Java codeString textString = "KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";

String tempString = textString.replaceAll(" ", "");

int wordNum = (textString.length - tempString.length) + 1;


这个不可取,如果字符串里有…
[/Quote]
maybe I need to replaceAll("[, ]", "");
lizhi1026 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 justinavril 的回复:]
Java codeString textString = "KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";

String tempString = textString.replaceAll(" ", "");

int wordNum = (textString.length - tempString.length) + 1;
[/Quote]

这个不可取,如果字符串里有其他的标点符号呢?
PS:还是2楼的好。
mengweilil 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 believefym 的回复:]
Java codeString str="KWA, where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";
System.out.println(str.split("[, ]+").length);
[/Quote]

我想,这个应该是最佳答案了,其实面试题就是考正则表达式的吧?

btw:,应该是,全角半角的区别,不过都是小问题了。
justinavril 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tt2451 的回复:]
Java code
String str = "KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";

String []a=str.split(" |,");
for(String sss:a)
{
System.out.println(sss);
}
System.out.println(a.length);





注意全半角…
[/Quote]
I assume that "world's" is one word. And also we know there is no "," in English, but "," does exist.
tt2451 2008-09-09
  • 打赏
  • 举报
回复

String str = "KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";

String []a=str.split(" |,");
for(String sss:a)
{
System.out.println(sss);
}
System.out.println(a.length);



注意全半角标点
believefym 2008-09-09
  • 打赏
  • 举报
回复

String str = "KWA, where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";
System.out.println(str.split("[, ]+").length);
justinavril 2008-09-09
  • 打赏
  • 举报
回复
String textString = "KWA,where one-fifth of the world's population in the fastest growing economy in the world has a rapidly growing desire and income to consume healthcare products from around the world";

String tempString = textString.replaceAll(" ", "");

int wordNum = (textString.length - tempString.length) + 1;

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧