我有两个字符串:
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(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 */
}
============================加密.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;
*/
}