Encryption & Decryption

I have encountered some problem here.
What I wanna do is to encrypt a sentences by invert the letters.
Eg, i love you -> r olev blf
However, I don't know what's wrong with my coding, the letter a and b unable to convert. Please check out my code:


#include <iostream >
using namespace std;
void encrypt( char [ ] ); // prototypes of functions used in the code
void decrypt( char [] );
int main( )
{
// create a string to encrypt
char string[ ] = "zyxwvust";
cout << "Original string is: " << string << endl;
// call to the function encrypt( )
cout << "Encrypted string is: ";
encrypt( string );
cout<<string<<endl;
cout<<"Decrypted string is: ";
decrypt (string);
cout<<string<<endl;
return 0;
}// main

//encrypt data
void encrypt (char e[] )
{
for( int i=0; e[i] != '\0'; ++i )
{switch (e[i])
{
case 'a':
case 'A':
e[i]+=25;
case 'b':
case 'B':
e[i]+=23;
case 'c':
case 'C':
e[i]+=21;
break;
case 'd':
case 'D':
e[i]+=19;
break;
case 'e':
case 'E':
e[i]+=17;
break;
case 'f':
case 'F':
e[i]+=15;
break;
case 'g':
case 'G':
e[i]+=13;
break;
case 'h':
case 'H':
e[i]+=11;
break;
case 'i':
case 'I':
e[i]+=9;
break;
case 'j':
case 'J':
e[i]+=7;
break;
case 'k':
case 'K':
e[i]+=5;
break;
case 'l':
case 'L':
e[i]+=3;
break;
case 'm':
case 'M':
e[i]+=1;
break;
case 'n':
case 'N':
e[i]-=1;
break;
case 'o':
case 'O':
e[i]-=3;
break;
case 'p':
case 'P':
e[i]-=5;
break;
case 'q':
case 'Q':
e[i]-=7;
break;
case 'r':
case 'R':
e[i]-=9;
break;
case 's':
case 'S':
e[i]-=11;
break;
case 't':
case 'T':
e[i]-=13;
break;
case 'u':
case 'U':
e[i]-=15;
break;
case 'v':
case 'V':
e[i]-=17;
break;
case 'w':
case 'W':
e[i]-=19;
break;
case 'x':
case 'X':
e[i]-=21;
break;
case 'y':
case 'Y':
e[i]-=23;
break;
case 'z':
case 'Z':
e[i]-=25;
break;
case ',':
e[i] = e[i];
break;
case '.':
e[i] = e[i];
break;
case '?':
e[i] = e[i];
break;
case ' ':
e[i] = e[i];
break;
case'!':
e[i] = e[i];
break;
case'\0':
i = 50;
break;
}
}
} // encrypt

void decrypt( char f[] ) {
for( int j=0; f[j] != '\0'; ++j )
{switch (f[j])
{
case 'a':
case 'A':
f[j]+=25;
case 'b':
case 'B':
f[j]+=23;
case 'c':
case 'C':
f[j]+=21;
break;
case 'd':
case 'D':
f[j]+=19;
break;
case 'e':
case 'E':
f[j]+=17;
break;
case 'f':
case 'F':
f[j]+=15;
break;
case 'g':
case 'G':
f[j]+=13;
break;
case 'h':
case 'H':
f[j]+=11;
break;
case 'i':
case 'I':
f[j]+=9;
break;
case 'j':
case 'J':
f[j]+=7;
break;
case 'k':
case 'K':
f[j]+=5;
break;
case 'l':
case 'L':
f[j]+=3;
break;
case 'm':
case 'M':
f[j]+=1;
break;
case 'n':
case 'N':
f[j]-=1;
break;
case 'o':
case 'O':
f[j]-=3;
break;
case 'p':
case 'P':
f[j]-=5;
break;
case 'q':
case 'Q':
f[j]-=7;
break;
case 'r':
case 'R':
f[j]-=9;
break;
case 's':
case 'S':
f[j]-=11;
break;
case 't':
case 'T':
f[j]-=13;
break;
case 'u':
case 'U':
f[j]-=15;
break;
case 'v':
case 'V':
f[j]-=17;
break;
case 'w':
case 'W':
f[j]-=19;
break;
case 'x':
case 'X':
f[j]-=21;
break;
case 'y':
case 'Y':
f[j]-=23;
break;
case 'z':
case 'Z':
f[j]-=25;
break;
case ',':
f[j] = f[j];
break;
case '.':
f[j] = f[j];
break;
case '?':
f[j] = f[j];
break;
case ' ':
f[j] = f[j];
break;
case'!':
f[j] = f[j];
break;
case'\0':
j = 50;
break;
}
}

} // encrypt


Furthermore, please do teach me how to input a text file to replace with my string. Which means, the letter I entered "zyxwvust" is replaced by a text file which the file name is entered by user,

Thanks you guys, if any further information needed, please do find me. Thanks and appreciate. =)
It is hard to read your code, because you used [output] blocks instead of code blocks and you did not use any indentation or whitespace in your code. Compare:

Good

[code]
#include <iostream>
using namespace std;

int main()
  {
  cout << "Hello world!\n";
  return 0;
  }
[/code]

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
  {
  cout << "Hello world!\n";
  return 0;
  }

Bad

#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!\n";
return 0;
}

For even this simple program the difference in readability is sufficiently pronounced that people won't want to deal with reading/deciphering it.


For your first problem: Don't use a switch statement; use a lookup string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const char encode_lookup[ 26 ][ 2 ] = 
  {
  "zyxwvutsrqponmlkjihgfedcba",
  "ZYXWVUTSRQPONMLKJIHGFEDCBA"
  };

char encode_char( char c )
  {
  // only alphabetic characters get changed
  if (isalpha( c ))
    {
    int index0 = toupper( c ) - 'A';  // get the new letter
    int index1 = isupper( c );        // get the proper case (lower or upper)
    return encode_lookup[ index0 ][ index1 ];
    }
  // non-alphabetic characters do not get changed
  return c;
  }

To decode, go the other way. Either make yourself a decode_lookup[] or simply search for the character in the lookup and turn it back into the original character by adding its index to 'A' or 'a', as appropriate.


For your second question, use a std::string and getline() to read input from the user (and to manipulate your variables):

1
2
3
4
5
  string plaintext;
  cout << "Please enter the message to encode> ";
  getline( cin, plaintext );
  string ciphertext = encode( plaintext );
  cout << "The encoded message is: " << ciphertext << "\n";

Notice how the functions now take std::strings instead of char*; this will make your life so much easier:

1
2
3
4
5
6
7
8
string encode( string s )
  {
  for (int i = 0; i < s.length(); i++)
    {
    s[ i ] = ...;
    }
  return s;
  }


Finally, as a word of caution, avoid naming your variables things like 'string' and 'e'. The first names a type, and in this case, it conflicts with a standard type. The second doesn't mean anything. The only time you should use one-letter variable names is when handling meta-syntactic parameters where the name is not important, such as:

  s          -- a string
  i, n, x, y -- integers
  f          -- a file
  b          -- a boolean
  c          -- a character

Otherwise, be sure to give your variables meaningful names, like "plaintext" or "users_input" or the like.

Hope this helps.
Topic archived. No new replies allowed.