注册 登录
编程论坛 C++教室

字符串反序输出问题

Waiting159 发布于 2008-03-06 12:59, 4687 次点击
程序代码:
/* 字符串反序存入另一个字符串,但输出时出现了问题 */
#include <iostream>
using namespace std;
#include <string>
int main(){
    string s1, s2;
   
    cout << "输入字符串:" << endl;
    cin >> s1;
   
    int len = s1.size();
    for(int i = len; i > 0; i--)
            s2[len - i] = s1[i - 1];
            
    /* 输出 */
            
    //for(int j = 0; j < len; j++)//方法一:这样一个一个输出就没有问题
      //      cout << s2[j];
   
    cout << endl << s2;         //方法二:这样则什么都不输出,为什么?
   
   
    system("pause");
    return 0;
}
11 回复
#2
sunkaidong2008-03-06 13:25
#include <iostream>
using namespace std;
#include <string>
int main(){
    string s1,s2;   
    cout << "输入字符串:" << endl;
    cin >> s1;
    s2 = s1;     
    cout<<s2<< endl;
    system("pause");
    return 0;
}
#3
sunkaidong2008-03-06 13:26
我不怎么用这些后引入数据类型.但我觉得既然定义了类,一定会对=重载所以没必要那么麻烦...
#4
Waiting1592008-03-06 13:28
楼上没有实现反序啊,什么意思?
#5
Ethip2008-03-06 13:51
回复 1# 的帖子
你只是把原来的数组元素逆序的复制到了另外一个数组s2,数组s1仍然没有改变!

你的意思应该是将s1中的元素都倒过来吧?
#6
aipb20072008-03-06 14:09
第一你没有反转
第二你的代码有严重的缺陷,string s,然后就s[index],运气好没事,运气不好直接内存错误,没有输出也是这个原因吧!
#7
Waiting1592008-03-06 14:21
/* 字符串反序存入另一个字符串,但输出时出现了问题 */
#include <iostream>
using namespace std;
#include <string>
int main(){
    string s1, s2;
   
    cout << "输入字符串:" << endl;
    cin >> s1;
   
    int len = s1.size();
    for(int i = len; i > 0; i--)
            s2[len - i] = s1[i - 1];
            
    /* 输出 */
            
    for(int j = 0; j < len; j++)//方法一:这样一个一个输出就没有问题
            cout << s2[j];
   
    //cout << endl << s2;         //方法二:这样则什么都不输出,为什么?
   
   
    system("pause");
    return 0;
}


我不是要将s1反序,我只是要将s1反序存储到中s2,然后输出s2,上面的代码就是可以的,请大家与原来的代码比较一下,看看为什么.
#8
hylhp2008-03-06 14:41
方法2:改为 cout<<s2<<endl;
#9
sunkaidong2008-03-06 14:47
#include <iostream>
using namespace std;
#include <string>
int main(){
    string s1,s2;   
    cout << "输入字符串:" << endl;
    s1.c_str();
    cin >> s1;
    for(int i=s1.size()-1;i>=0;i--)
    {
        s2+=*(s1.c_str()+i);
    }
    s1=s2;
    cout<<s1<< endl;
    system("pause");
    return 0;
}
#10
sunkaidong2008-03-06 15:16
#include <iostream>
using namespace std;
#include <string>
class string1:public string
{
public:
    string1 &operator-=(string1& s)
    {   string1 s2;
        for(int i=s.size()-1;i>=0;i--)
    {
        s2+=*(s.c_str()+i);
    }
        s=s2;
        *this=s;
        return s;
    };
};
int main(){
    string1 s1,s;   
    cout << "输入字符串s1:" << endl;
    cin >> s1;
     s-=s1;
     cout << "输出字符串s1:" << endl;
    cout<<s1<< endl;
     cout << "输出字符串s:" << endl;
    cout<<s<< endl;
    
    system("pause");
    return 0;
}

[[it] 本帖最后由 sunkaidong 于 2008-3-6 15:22 编辑 [/it]]
#11
sunkaidong2008-03-06 15:35
#include <iostream>
using namespace std;
#include <string>
class string1:public string
{
public:
    string1 &operator~()
    {   string1 s2;
        for(int i=(*this).size()-1;i>=0;i--)
    {
        s2+=*((*this).c_str()+i);
    }
        //s=s2;
        *this=s2;
        return *this;
    };
};
int main(){
    string1 s1,s;   
    cout << "输入字符串s1:" << endl;
    s1.c_str();
    cin >> s1;
    cout << "输出字符串s1:" << endl;
    cout<<~s1<< endl;    
    system("pause");
    return 0;
}
#12
PcrazyC2008-03-06 15:55
如果你对STRING类型不熟的话,我建议你改成用数组来完成
1