简体   繁体   中英

How can I find if a character has already been used in a string?

I am trying to create a substitution cipher. I am doing it so that the whole alphabet is changed based on a random number based off of a seed. I will then use the new alphabet to encrypt the message that is passed into the function. I have tried using find() and contains() but since i am trying to compare a character to another character it does not work.

int main() {
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string newAlphabet;
int alphabetSize = alphabet.size();

for (int i=0; i < alphabetSize; i++) {
    int offset = rand() % 26 +1;

    if (newAlphabet[i].find(alphabet[i])) {
        newAlphabet = alphabet[offset];
        cout<<newAlphabet;
    }
}
return 0;

}

This is the code I have been trying to use. This is not the whole code just what I am using to cipher the alphabet. Please help me I have been working on this for hours.

you are looking in an uninitialized string, you must first initialize newAlphabet and only then can you do newAlphabet.find (...). Otherwise the statement of the if will always be False.

string newAlphabet = <SOMETHING>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM