ascii

This is a discussion on ascii within the C++ Programming forums, part of the General Programming Boards category; for a project i need to encrypt a message and have the encrypted version be the next character in the ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    ascii

    for a project i need to encrypt a message and have the encrypted version be the next character in the alphabet (like secret would be tfdsfu) what code would i use for that?

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Well you are going to have to take every letter and change it with another letter, then store in an array/string and display it.

    Begin with a function that encrypts it and it returns the ectrypted word.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    this is what i have so far.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    void encrypt ( char e[] );
    void decrypt ( char *ePtr );
    
    int main()
    {
       // create a string to encrypt
       char string[] = "this is a secret!";
    
       cout << "Original string is: " << string << endl; 
    
       encrypt( string );
       cout << "Encrypted string is: " << string << endl;
    
       decrypt( string );
       cout << "Decrypted string is: " << string << endl;
    
       return 0;
    
    } // end main
    
    // encrypt data
    void encrypt( char e[] )
    {
       ( e += 1);
    
    } // end function encrypt
    
    // decrypt data
    void decrypt( char *ePtr )
    {
       (*ePtr -= 1);
    
    } // end function decrypt

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The encrypt function is wrong simply because you've made it an array. The syntax is incorrect. (Well, arrays do degrade into a pointer, but it's ugly and the syntax is still wrong.) Also, both of those only modify one character. You need to loop through the entire string. Either make both functions loop, or make the two functions titled something more appropriate, like 'encryptChar' 'decryptChar', and call them repeatedly.

    Also the parenthesis in both of those is not needed.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    It's only modifying the first letter. How do i modify the function so that it will modify all of the letters?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    void encrypt ( char e[] );
    void decrypt ( char *ePtr );
    
    int main()
    {
       // create a string to encrypt
       char string[] = "this is a secret!";
    
       cout << "Original string is: " << string << endl; 
    
       encrypt( string );
       cout << "Encrypted string is: " << string << endl;
    
       decrypt( string );
       cout << "Decrypted string is: " << string << endl;
    
       return 0;
    
    } // end main
    
    // encrypt data
    void encrypt( char e[] )
    {
       *e += 1;
    
    } // end function encrypt
    
    // decrypt data
    void decrypt( char *ePtr )
    {
       *ePtr -= 1;
    
    } // end function decrypt

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to loop through all of the characters in a string? If not, that's what you need to figure out first. Also, you shouldn't name your variables 'string', because C++ has a string class, which oddly enough is called string.

    Here's a hint:

    All strings are terminated with a null character (the '\0'). So, you can simply loop through the string until you find it. When you do, you're at the end and can stop.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    how do you loop through all characters in a string?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >how do you loop through all characters in a string?
    Here's an example.
    Code:
       char message[] = "this is a secret!";
       for (i=0; message[i] != '\0'; ++i)
          cout << message[i];
       cout << endl

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use an index to keep track of which char of the string you are evaluating. Start the evaluation at index = 0. Discontinue the loop when value of current char is '\0' or when index is equal to length of string, whichever you prefer. Remember to add the terminating null char at index equal to the length of original string in the encrypted version so that the encrypted version is a string and not a simple char array. You don't want to encrypt the null char. If you donn't know how to make a loop out of this description I'd refer you to the tutorial for a discussion of the various options available.

    The last issue you will need to decide is how to handle z. Do you change it to an a or to whatever comes after z in the ASCII table?
    You're only born perfect.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    I'm still having trouble. The bolded parts is what i think is wrong. What do i need to do to them?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    void encrypt ( char e[] );
    void decrypt ( char *ePtr );
    
    int main()
    {
       // create a string to encrypt
       char string[] = "this is a secret!";
    
       cout << "Original string is: " << string << endl; 
    
       encrypt( string );
       cout << "Encrypted string is: " << string << endl;
    
       decrypt( string );
       cout << "Decrypted string is: " << string << endl;
    
       return 0;
    
    } // end main
    
    // encrypt data
    void encrypt( char e[] )
    {
    	int i;
    	for (i=0; i<strlen(e); i++ );
    	{ 
    		(e[i] +=1); 
    	}
    } // end function encrypt
    
    // decrypt data
    void decrypt( char *ePtr )
    {
    	ePtr [0] -= 1;
    	ePtr [1] -= 1;
    	ePtr [2] -= 1;
    	ePtr [3] -= 1;
    	ePtr [4] -= 1;
    	ePtr [5] -= 1;
    	ePtr [6] -= 1;
    	ePtr [7] -= 1;
    	ePtr [8] -= 1;
    	ePtr [9] -= 1;
    	ePtr [10] -= 1;
    	ePtr [11] -= 1;
    	ePtr [12] -= 1;
    	ePtr [13] -= 1;
    	ePtr [14] -= 1;
    	ePtr [15] -= 1;
    	ePtr [16] -= 1;
    
    } // end function decrypt

  11. #11
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by fenixataris182
    Code:
    void encrypt( char e[] )
    {
    	int i;
    	for (i=0; i<strlen(e); i++ );
    	{ 
    		(e[i] +=1); 
    	}
    }
    This semicolon makes the for loop an empty loop. You want to remove that semicolon.

    Also, realize that you will be calling strlen over and over again, each time, when you really only need to call it once. This is a pretty big inefficiency. You could write:

    Code:
    void encrypt( char e[] )
    {
    	int i;
    	int len = strlen(e);
    	for (i = 0; i < len; i++ ) { 
    		e[i] += 1; 
    	}
    }
    Code:
    // decrypt data
    void decrypt( char *ePtr )
    {
    	ePtr [0] -= 1;
    	ePtr [1] -= 1;
    	ePtr [2] -= 1;
    	ePtr [3] -= 1;
    	ePtr [4] -= 1;
    	ePtr [5] -= 1;
    	ePtr [6] -= 1;
    	ePtr [7] -= 1;
    	ePtr [8] -= 1;
    	ePtr [9] -= 1;
    	ePtr [10] -= 1;
    	ePtr [11] -= 1;
    	ePtr [12] -= 1;
    	ePtr [13] -= 1;
    	ePtr [14] -= 1;
    	ePtr [15] -= 1;
    	ePtr [16] -= 1;
    
    } // end function decrypt
    I assume you know what's wrong with this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  2. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21