-2

我有两个字符串:

string word;
string alphabet;

word有一些我传递给的输入;比方说"XYZ"

alphabet有字母 except 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(alfabe,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 */
    }

============================加密.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);
4

3 回答 3

1

+=是你怎么做的,你一定是在做别的事情。

string word = "XYZ";
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVW";
alphabet += word;
cout << alphabet << "\n";

输出

ABCDEFGHIJKLMNOPQRSTUVWXYZ

于 2013-04-10T12:10:12.090 回答
0

利用

单词。附加(字母);

请注意,当您想要连接许多字符串时,使用字符串流来避免不必要的字符串复制会更有效

于 2013-04-10T12:04:28.120 回答
0
string word = "XYZ";

string alphabet = "ABCDEFGHIJKLMNOPQRSTUVW";

word += alphabet;

现在您可以打印“word”,您将找到答案。

于 2013-04-10T12:11:35.747 回答