2

I have two arrays of the alphabet which have been sorted in two different ways and in the order i need. now i need to replace, in order, the first letter in indexb[] with the first letter in indexa[] all the way to the 26th letter of them both. i have used the replace() function to then change the letters in the text that i need to swap the letters for and then output the message with all the swaps. but the code is not swapping the right letters i want swapping. any advice?

                char c;
                vector<char> fileChars;
                while (code.good())                                         
                {
                    code.get(c);
                    fileChars.push_back(c);

                }
                for (int i = 0; i < 26; i++) 
                {
                    replace(fileChars.begin(), fileChars.end(),indexb[i],indexa[i]);
                }

                for (int i = 0; i < fileChars.size(); i++)
                {
                    decrypted<< fileChars[i];
                }
6
  • don't really get what you try to achieve. I assume you have a text where you'd like to replace every char by another one (decryption). is that right? Commented Dec 12, 2011 at 17:21
  • Would you post more of your code, like the definition of code, indexa, indexb, and so on? Commented Dec 12, 2011 at 17:27
  • Thats's right, i have seen other replacement methods but i have just written hundreds of lines of code to obtain the two arrays that i need in the the order. the letters are replacing but in indexb[] first letter it finds the letter in indexa[] that i want it replaced with, but then it finds that letter in indexb[] and replaces the letter in message with the corrisponding letter in indexa[], hope that makes sense. Commented Dec 12, 2011 at 17:27
  • You want the whole code its like 300 lines? Commented Dec 12, 2011 at 17:40
  • Why are you using arrays? Do you have something against std::string? Commented Dec 12, 2011 at 17:56

3 Answers 3

3

The other answers, while possible appear to be slow and inefficient.

The best way I can think of is to:

  1. Loop over each character in the "encrypted" text file
  2. Find out at which element the current character lies in your "frequency" array. You already have the find_in_array() function to help you do this.
  3. Write the character that lies in the index of your "encrypted" rank array

You obviously will want to throw in some basic error checking to ensure that non-alpha characters are not looked for in your two index arrays.

Some example C++ to get you going would be:

char c;
int pos;
while (code.good())
{
    code.get(c);
    if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
    {
        pos = find_in_array(indexb, c, 26);
        decrypted << indexa[pos];
    }
    else
    {
        decrypted << c;
    }
}

Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

just put this in the code and works a treat! ill look into thoses errors, thanks!
2

Unless you'd like to use wchar_t for your characters you're overcomplicating it. You can get away with one or two arrays (two arrays only if you'd like to encode and decode the fast way; it's also possible to do everything with one array (similar to what you tried to do), but that's slower and more expensive performance wise).

The idea is, doing something like the following. You should be able to do this without doing any significant changes to your existing code (just change the way your arrays are set up and used).

char encoder[256];
char decoder[256];

Now generate your dictionary in whatever way you do it, for each character you should get the following two variables you'd store in those arrays:

char from = 'a'; // the unencoded character
char to = 'x'; // the encoded character

// store them in the arrays for later use:
encoder[from] = to;
decoder[to] = from;

That's it! To encode a string, do the following:

// these two could be pointers to buffers too
char my_string[] = "Hello World!";
char my_string_enc[256];

unsigned int p = 0;

while(my_string[p])
    my_string_enc[p] = encoder[my_string[p++]];
my_string_enc[p] = '\0'; // null terminating the encoded string

Decoding can be done in a similar fashion:

// these two could be pointers to buffers too
char my_string_enc[] = "...";
char my_string[256];

unsigned int p = 0;

while(my_string_enc[p])
    my_string[p] = decoder[my_string_enc[p++]];
my_string[p] = '\0'; // null terminating again

1 Comment

hey thanks for posting i can see u put great effort into this and have help me understand this more thank you!
1

It might be better to go through the string you want to decrypt one character at a time and change the characters. so:

for (int i = 0; i < fileChars.size(); i++)
{
    for(int j = 0; j < 26; j++){
        if(fileChars[i]==indexb[j]){
            fileChars[i]=indexa[j];
        }
    }
}

It's not as efficient with the nested loop but it would work.

1 Comment

thanks for ur post very helpful but i went with a more effficient way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.