将2个基本字符串连接在一起

时间:2013-04-11 13:06:15

标签: c++ string string-concatenation

我有两个字符串:

string word;
string alphabet;

word有一些我传递给的内容;让我们说"XYZ"

alphabet的字母除了XYZ,所以它是"ABCDEFGHIJKLMNOPQRSTUVW"

当我尝试用“+ =”连接它们时,我得到的只是word(即"XYZ")。我想让它看起来像这样:

XYZABCDEFGHIJKLMNOPQRSTUVW 

我做错了什么?代码基本上就是这个vvvv

=========================== encryption.cpp ================= ================================

#include <cstdlib>
#include <iostream>
#include "encryption.h"
#include <string>

encryption::encryption()
{
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

}


string encryption::removeletter(string word,char letter)
{
    //remove letter
    string temp;
    int indis=0;
    for(int i = 0; word[i] != '\0'; i++)
    {
        if(word[i] != letter)
            {
                temp[indis]=word[i] ;
                indis++;
            }

    }

    word=temp;

    return word;
}

This is the function i have proplems with :

    void encryption::encrypt(string word)//main.cpp is just calling this atm
    {
        string temp;
        int pos;
         //getting rid of the repeating letters
        for(int i = 0; i < word.length(); i++)
        {
            if( (pos = temp.find(kelime[i])) < 0)
                temp += word[i];
        }
        word=temp;//that ends here
        //taking words letters out of the alphabet
        for(int i = 0; i < word.length(); i++)
        {
            alfabet=removeletter(alfabet,word[i]);

        }
        for(int i = 0; i < word.length()-1; i++)
        {
            for(int j=0;alfabet[j] !='\0'; j++)
                if(alfabet[j+1] =='\0') alfabet[j]='\0';
        }

        /* I tried += here */
    }

=========================== encryption.h ================= ===================================

#ifndef encryption_h
#define encryption_h
#include<string>

    class encryption

    {
    public:
        encryption();

        void encrypt(string word);
        string removeletter(string word,char letter);
        //some other functions,i deleted them atm

    public:
            string alphabet;
            string encryptedalphabet;
            //staff that not in use atm(deleted)

    };

#endif

===========================的main.cpp ================= =====================================

#include <cstdlib>
#include <iostream>
#include "encryption.h"
#include <string>

string word;
encryption encrypted;
cin>>word;
encrypted.encrypt( word);





 /****************************************************************************
orginalcode

**cikarmasifresi.cpp
****************************************************************************/

#include <cstdlib>
#include <iostream>
#include "cikarmasifresi.h"
#include <string>


using namespace std;

cikarmasifresi::cikarmasifresi()
{
alfabe = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

}



string cikarmasifresi::yazisifrele(string yazi)
{
    for(int j=0;yazi[j] != '\0'; j++)
    {
            for(int i = 0; alfabe[i] != '\0'; i++)
            {
                if(yazi[j]==alfabe[i])
                {
                    yazi[j]=sifrealfabesi[i];
                }

            }
    }

    return yazi;
}
string cikarmasifresi::sifrecoz(string sifreliyazi)
{
    for(int j=0;sifreliyazi[j] != '\0'; j++)
    {
            for(int i = 0; sifrealfabesi[i] != '\0'; i++)
            {
                if(sifreliyazi[j]==sifrealfabesi[i])
                {
                    sifreliyazi[j]=alfabe[i];
                }

            }
    }

    return sifreliyazi;
}
string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    int indis=0;
    for(int i = 0; kelime[i] != '\0'; i++)
    {
        if(kelime[i] != harf)
            {
                temp[indis]=kelime[i] ;
                indis++;
            }

    }

    kelime=temp;

    return kelime;
}


void cikarmasifresi::sifrelialfabeolustur(string kelime)
{
    string temp,temp2;
    sifrekelimesi=kelime;

    int pos;
    for(int i = 0; i < kelime.length(); i++)
    {
        if( (pos = temp.find(kelime[i])) < 0)
            temp += kelime[i];
    }
    kelime=temp;

    for(int i = 0; i < kelime.length(); i++)
    {
        alfabe=harfcikar(alfabe,kelime[i]);

    }
    for(int i = 0; i < kelime.length()-1; i++)
    {
        for(int j=0;alfabe[j] !='\0'; j++)
            if(alfabe[j+1] =='\0')  alfabe[j]='\0';

    }


     sifrealfabesi=kelime+alfabe;




        cout<<sifrealfabesi;

    cout<<endl;
    cout<<endl;
        for(int i = 0; kelime[i] != '\0'; i++)
    {
        cout<<kelime[i];
    }






}





/****************************************************************************
** orginalcode

**cikarmasifresi.h
****************************************************************************/


#ifndef cikarmasifresi_h
#define cikarmasifresi_h
#include<string>

using namespace std;

class cikarmasifresi
{
public:
    cikarmasifresi();

    void sifrelialfabeolustur(string kelime);
    string yazisifrele(string yazi);
    string sifrecoz(string sifreliyazi);
    string harfcikar(string kelime,char harf);
public:
        string alfabe;
        string sifrealfabesi;
        string sifrekelimesi;

};

#endif





/****************************************************************************
 orginal code

**main.cpp
****************************************************************************/

#include <cstdlib>
#include <iostream>
#include "cikarmasifresi.h"
#include <string>

using namespace std;

int main()
{

        system("cls");
        string kelime,yazi,sifreli;
        cikarmasifresi sifrele;
        cout<<"********************************************************************************"<<endl;
        cout<<"sifre kelimesi gir :"<<endl;
        cin>>kelime;
        sifrele.sifrelialfabeolustur(kelime);/*
        cout<<"********************************************************************************"<<endl;
        cout<<"Alfabe  :"<<sifrele.alfabe<<endl;
        cout<<"********************************************************************************"<<endl;
        cout<<"Sifreli Alfabe :"<<sifrele.sifrealfabesi<<endl;
        cout<<"********************************************************************************"<<endl;
        cout<<"Cumle gir :"<<endl;
        cin>>yazi;

        cout<<"********************************************************************************"<<endl;
        cout<<"Sifre :"<< sifrele.yazisifrele(yazi)<<endl;
        cout<<"********************************************************************************"<<endl;
        cout<<"sifreli yazi gir :"<<endl;
        cin>>sifreli;
        cout<<"********************************************************************************"<<endl;
        cout<<"Sonuc :"<<sifrele.sifrecoz(sifreli)<<endl;

        */




}

1 个答案:

答案 0 :(得分:1)

这是错误的

string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    int indis=0;
    for(int i = 0; kelime[i] != '\0'; i++)
    {
        if(kelime[i] != harf)
            {
                temp[indis]=kelime[i] ;
                indis++;
            }

    }

    kelime=temp;

    return kelime;
}

应该是

string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    int indis=0;
    for(int i = 0; i < kelime.length(); i++)
    {
        if(kelime[i] != harf)
            {
                temp[indis]=kelime[i] ;
                indis++;
            }

    }

    kelime=temp;

    return kelime;
}

kelime是一个std :: string。它没有像C字符串那样的空终止符。

这些功能以完全相同的方式出错

string cikarmasifresi::sifrecoz(string sifreliyazi)

string cikarmasifresi::yazisifrele(string yazi)

这是一个不同的错误

string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    int indis=0;
    for(int i = 0; i < kelime.length(); i++)
    {
        if(kelime[i] != harf)
            {
                temp[indis]=kelime[i] ;
                indis++;
            }

    }
    kelime=temp;
    return kelime;
}

temp是一个零长度字符串,因此temp[indis]是一个错误。您可能意味着temp += kelime[i]喜欢这个

string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    for(int i = 0; i < kelime.length(); i++)
    {
        if(kelime[i] != harf)
            {
                temp += kelime[i];
                indis++;
            }

    }
    kelime=temp;
    return kelime;
}

这是第三个错误

for(int i = 0; i < kelime.length()-1; i++)
{
    for(int j=0;alfabe[j] !='\0'; j++)
        if(alfabe[j+1] =='\0')  alfabe[j]='\0';

}

我真的不知道你在这里要做什么,所以我不能建议修复。也许你应该删除这段代码。