Nother
程序员,规则的,陈述者! Programmer is Representor Of Rules!
消除标点符号
编一个程序,从string 对象中去掉标点符号。要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号后的string 对象。
消除标点
#include
<
iostream
>
#include
<
string
>
#include
<
cctype
>
using
namespace
std;
int
main()
{
string
s, result_str;
bool
has_punct
=
false
;
//
用于标记字符串中有无标点
char
ch;
//
输入字符串
cout
<<
"
Enter a string:
"
<<
endl;
getline(cin, s);
//
处理字符串:去掉其中的标点
for
(
string
::size_type index
=
0
; index
!=
s.size();
++
index)
{
ch
=
s[index];
if
(ispunct(ch))
has_punct
=
true
;
else
result_str
+=
ch;
}
if
(has_punct)
cout
<<
"
Result:
"
<<
endl
<<
result_str
<<
endl;
else
{
cout
<<
"
No punctuation character in the string?!
"
<<
endl;
return
-
1
;
}
return
0
;
}
posted on
2008-04-24 16:29
哪热
阅读(149) 评论(
2
)
编辑
收藏
刷新评论
刷新页面
返回顶部
程序员问答社区,解决您的IT难题
博客园首页
博问
新闻
闪存
程序员招聘
知识库
Powered by:
博客园
Copyright © 哪热
导航
博客园
首页
新随笔
联系
订阅
管理
统计
随笔 - 62
文章 - 0
评论 - 73
引用 - 0
公告