请各位筒子帮个忙解决方案
请各位筒子帮个忙
[code=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 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习题我自己改的要求
------解决方案--------------------
[code=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 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习题我自己改的要求
------解决方案--------------------
- 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 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; }
------解决方案--------------------
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;
}
}
这个循环中如果后两位都是符号的话会陷入死循环,,,,