私は2つの文字列を持っています:
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;
*/
}