Passing pointers to functions and manipulating...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NC AQUA
    New Member
    • Apr 2007
    • 4

    Passing pointers to functions and manipulating...

    Evening everyone...

    First, this is a homework assignment and I am not looking for the direct answer (notice no code...), but instead I need direction to help figure this out. I am new to this and do not plan on continuing after this class. Not my forte...

    I have to encrypt and decrypt a char pointer. I have passed the pointer to the encrypt function (debug code reveals - output statement). I need to increase the value by 1 in the encrypt function and then decrease by one in the decrypt function.

    To do this I need to convert the characters to ASCII, then add 1 and then reconvert back to characters, right? Can someone provide guidance on how to do this? Then to do the decrypt, I pass the returned value as a pointer from the encrypt function to the decrypt function and do that conversion, right?

    I will post code if need be...but don't it is necessary at this point.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The C/C++ char type is actually just an integer. If you display it on the screen it will appear as a character from the ASCII character set (or whatever character set your platform happens to use) however internally in the code there is no need to convert it to and from ASCII or any other character set.

    so this code

    Code:
        char c = 'A';
    
        cout << c << endl;
    
        c++;
    
        cout << c << endl;
    outputs

    A
    B

    assuming that the character set in use is ASCII. It should be noted that not all character sets have the letters in consecutive order and that not all C/C++ implementations use ASCII.

    What this means is that this code is not portable to all platforms, a fully portable version might look something like

    Code:
        char c = 'A';
    
        cout << c << endl;
    
        if (c == 'A')
        {
            c = 'B';
        }
    
        cout << c << endl;
    However people often do assume that the ASCII character set is in use (and it is becoming more an more prevalent) and use the first example.

    Comment

    • NC AQUA
      New Member
      • Apr 2007
      • 4

      #3
      Ok...thanks that does help a little bit. Now I know I don't need to perfom any conversions, thanks.

      However, I need to increase the value of my variable (a statement) by one letter.

      For example:

      hello world!
      ifmmp!xpsme"

      Each letter in the hello world statement is increased by a value of one letter.

      I have the statement reach my function via pointer. From here I am stuck. Do I need to create a separate function? I know I could probably create huge code where a = b, b = c, c = d, d = e, etc, but I know that there has got to be an easier and cleaner way to code this.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by NC AQUA
        I have the statement reach my function via pointer. From here I am stuck. Do I need to create a separate function? I know I could probably create huge code where a = b, b = c, c = d, d = e, etc, but I know that there has got to be an easier and cleaner way to code this.
        Like I said, portably no there isn't, about the only way to do it is 1 huge switch statement.

        However (also like I said) if you assume ASCII that you can just add the 1 onto the currently value (unless it's z).

        You could create a function that takes a character and returns the encoded character or you could just operate directly on the data pointed to.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          A char is an integer.

          That means you can do arithmetic on it.

          Code:
          char data = 'A';
          cout << data;  //displays A
          ++data;
          cout << data; //displays B
          The ASCII table cleverly has the upper and lower case alphabet arranged in a a numeric sequence.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by weaknessforcats
            A char is an integer.

            That means you can do arithmetic on it.

            Code:
            char data = 'A';
            cout << data;  //displays A
            ++data;
            cout << data; //displays B
            The ASCII table cleverly has the upper and lower case alphabet arranged in a a numeric sequence.
            But read Banfa's reply #2; not the entire world is ASCII. It's way better to keep
            I18N and L10N into mind (I18N == Internationaliz atioN, L10N = LocalizatioN).

            kind regards,

            Jos

            Comment

            Working...