请各位筒子帮个忙

443692487 2012-09-06 07:55:14

#include <iostream>
#include <string>
int main()
{
using std::string;
using std::cout;
using std::cin;
using std::endl;

string s;
cout<<"Please input s with punctuations! "<<endl;
std::getline(cin,s);

string::size_type total = s.size();
string::size_type i,count = 0;

for(i = 0;i<(total-1);i++)
{
if(ispunct(s[i]))
{
for(int j = i;j<total-1;j++)
s[j] =s[j+1] ;
++count;
if(i!=(total-2))
i = i-1;
}
}

if(ispunct(s[total-1]))
{
count++;
total = total -1;
}

cout<<"s now is : ";
for(i = 0;i <(total-count); i++)
cout<<s[i];
cout<<endl;
cout<<"count is : "<<count<<endl;
return 0;
}


小弟想写一个直接在string对象删除里面的标点的程序,可是这个程序运行的结果并不是我想要的。。。各位能不能给个思路啊?附上源代码也行,谢谢了啊!要求是不要建立新的string对象。其实这是primer上面3.10习题我自己改的要求
...全文
98 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr_defy_L 2012-09-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
引用 3 楼 的回复:
C/C++ code

#include <iostream>
#include <string>


int main()
{
using std::string;
using std::cout;
using std::cin;
using std::endl;

string s;
cout<<"Please……
谢谢大神啊,对的,移动……
[/Quote]

我是一只菜菜菜鸟,给分结贴吧
443692487 2012-09-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
for(i = 0;i<(total-1);i++)
{
if(ispunct(s[i]))
{
for(int j = i;j<total-1;j++)
s[j] =s[j+1] ;
++count;
if(i!=(total-2))
i = i-1;
}
}


这个循环中如果后两位都是符号的话会陷入死循环,,,,
[/Quote]
嗯嗯,是的,谢谢了啊 嘿嘿
thz1598746 2012-09-06
  • 打赏
  • 举报
回复
for(i = 0;i<(total-1);i++)
{
if(ispunct(s[i]))
{
for(int j = i;j<total-1;j++)
s[j] =s[j+1] ;
++count;
if(i!=(total-2))
i = i-1;
}
}


这个循环中如果后两位都是符号的话会陷入死循环,,,,
Mr_defy_L 2012-09-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
引用 3 楼 的回复:
C/C++ code

#include <iostream>
#include <string>


int main()
{
using std::string;
using std::cout;
using std::cin;
using std::endl;

string s;
cout<<"Please……
谢谢大神啊,对的,移动……
[/Quote]

俺依然是菜鸟,最后说一句,分给我
443692487 2012-09-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
C/C++ code

#include <iostream>
#include <string>


int main()
{
using std::string;
using std::cout;
using std::cin;
using std::endl;

string s;
cout<<"Please……
[/Quote]谢谢大神啊,对的,移动之后要把最后的字符改成空字符。。而且要total自减。。谢谢谢谢
Mr_defy_L 2012-09-06
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>


int main()
{
using std::string;
using std::cout;
using std::cin;
using std::endl;

string s;
cout<<"Please input s with punctuations! "<<endl;
std::getline(cin,s);

string::size_type total = s.size();
string::size_type i,count = 0;

for(i = 0;i<total;i++)
{
if(ispunct(s[i]))
{
for(int j = i;j<total-1;j++)
s[j] =s[j+1];

++count;
--i;
s[--total]=0;
}
}
cout<<"s now is : "<<s<<endl;

cout<<"count is : "<<count<<endl;
return 0;
}

443692487 2012-09-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
应该是<= total - 1或< total
[/Quote]
不是的。。这样也不对。我之前就试过了。。这个程序主要的问题是如果标点符号是最后一个字符的时候就会出错。谢谢您的回复
Gonefar 2012-09-06
  • 打赏
  • 举报
回复
应该是<= total - 1或< total

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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