我有两个字符串:
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);