会员
周边
新闻
博问
闪存
众包
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
代码缔造的帝国
博客园
首页
新随笔
联系
订阅
管理
java 统计单词个数和标点符号
把随机输入的一句话比如:It's only a test!存放在一个char[]的数组中,统计char[]中的单词个数和标点符号的个数。
package com.faintbear;
import java.io.
*
;
public
class
Test
{
public
static
void
main(String[] args) throws Exception
{
BufferedReader br
=
new
BufferedReader(
new
InputStreamReader(System.
in
));
String str
=
br.readLine();
if
(str
==
null
)
throw
new
Exception(
""
);
char
[] c
=
str.toCharArray();
int
words
=
0
;
int
ip
=
0
;
boolean wordflag
=
false
;
for
(
int
i
=
0
;i
<
c.length;i
++
)
{
if
((c[i]
>=
'
a
'
&&
c[i]
<=
'
z
'
)
||
(c[i]
>=
'
A
'
&&
c[i]
<=
'
Z
'
))
{
if
(wordflag)
{
continue
;
}
else
{
words
++
;
}
wordflag
=
true
;
}
else
{
wordflag
=
false
;
if
(c[i]
!=
'
'
)
ip
++
;
}
}
System.
out
.println(
"
words=
"
+
words);
System.
out
.println(
"
ip=
"
+
ip);
for
(
int
i
=
0
;i
<
c.length;i
++
)
{
System.
out
.print(
"
c[
"
+
i
+
"
]=
"
+
c[i]);
}
}
}
posted @
2005-11-28 12:49
代码缔造的帝国
阅读(
1701
) 评论(
1
)
收藏
举报
刷新页面
返回顶部
公告