永发信息网

C++ 可以帮我看看吗?删除大写字母和全改为大写字母

答案:3  悬赏:80  手机版
解决时间 2021-02-08 06:01
源程序:
-------------------------------------------
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;
main()
{
string ll("pasKoaskks");
string::iterator first=ll.begin(),last=ll.end();
string kk;
char oo;
int yy=0;
while(first!=last)//全改为大写并储存到kk
{
oo=toupper(*first);
kk=kk+oo;
++first;
}
char jjk;
int pp=0;
int hh=0;
cout<<ll<<endl;//证明ll并没被改变
cout<<"1"<<endl;
for(size_t pk=0;pk!=(int)ll.size();++pk)//删除大写
{
jjk=*first;
cout<<"2"<<endl;
if('A'<jjk<'Z')//如果是大写
{
ll.erase(*first);//除掉
first=ll.begin();//复位
++pp;//自增被删数
cout<<"3"<<endl;
for(int i=0;i<hh-pp;++i)//删除前已经过的
{
++first;
}
cout<<"4"<<endl;
}else{
++first;
}
cout<<"5"<<endl;
++hh;
}
cout<<pp<<endl;//被删除数
cout<<ll<<endl;
cout<<kk<<endl;
return 0;
}
--------------------------------------------
输出了:

ll=pasKoaskks
pp=10
ll=
kk=PASKOASKKS

其他的省略,
循环前ll还保持着原样,
是循环后才被删掉的,前一个改变成大写并没影响到原值,
很明显ll被全删掉了,怎么回事啊?是不是判断那出问题了?应该怎么改呢?

-------------------
最佳答案
C++ 可以帮我看看吗?删除大写字母和全改为大写字母
悬赏分:10 - 离问题结束还有 7 天 21 小时
源程序:
-------------------------------------------
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;
main()
{
string ll("pasKoaskks");
string::iterator first=ll.begin(),last=ll.end();
string kk;
char oo;
int yy=0;
while(first!=last)//全改为大写并储存到kk
{
oo=toupper(*first);
kk=kk+oo;
++first;
}
char jjk;
int pp=0;
int hh=0;
cout<<ll<<endl;//证明ll并没被改变
cout<<"1"<<endl;
for(size_t pk=0;pk!=(int)ll.size();++pk)//删除大写
{
jjk=*first;
cout<<"2"<<endl;
if('A'<jjk<'Z')//如果是大写
{
ll.erase(*first);//除掉
first=ll.begin();//复位
++pp;//自增被删数
cout<<"3"<<endl;
for(int i=0;i<hh-pp;++i)//删除前已经过的
{
++first;
}
cout<<"4"<<endl;
}else{
++first;
}
cout<<"5"<<endl;
++hh;
}
cout<<pp<<endl;//被删除数
cout<<ll<<endl;
cout<<kk<<endl;
return 0;
}
--------------------------------------------
输出了:

ll=pasKoaskks
pp=10
ll=
kk=PASKOASKKS
全部回答
# include <iostream> # include <string> # include <cctype> using namespace std; void to_upper(string::iterator first, string::iterator last) {     while (first != last)     {         *first = toupper(*first);         ++first;     } } void erase_upper(string::iterator first, string& s) {     while (first != s.end())     {         if (isupper(*first))             first = s.erase(first);         else             ++first;     } } int main() {     string s;     cout << "输入字符串 : ";     cin >> s;     int n;     cout << "将字符串改为大写, 按 1 . 删除字符串中的大写字母, 按 2 ." << endl;     while (1)     {         if (cin.fail())         {             cin.clear();             cin.sync();         }         cin >> n;         if (n == 1 || n == 2)             break;         else             cerr << "错误! 重试." << endl;     }     switch (n)     {     case 1:         to_upper(s.begin(), s.end());         break;     case 2:         erase_upper(s.begin(), s);         break;     }     cout << endl << s << endl;     return 0; }
c++ 可以帮我看看吗?删除大写字母和全改为大写字母 悬赏分:10 - 离问题结束还有 7 天 21 小时 源程序: ------------------------------------------- #include #include #include #include #include #include using namespace std; main() { string ll("paskoaskks"); string::iterator first=ll.begin(),last=ll.end(); string kk; char oo; int yy=0; while(first!=last)//全改为大写并储存到kk { oo=toupper(*first); kk=kk+oo; ++first; } char jjk; int pp=0; int hh=0; cout<
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯