Encryption program inside...

This is a discussion on Encryption program inside... within the C Programming forums, part of the General Programming Boards category; Hello, I know this is a bad program and I have thrown in many bugs related to type conversion. I ...

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Arrow Encryption program inside...

    Hello,

    I know this is a bad program and I have thrown in many bugs related to type conversion. I am getting a warning also. Can you help me correct it ?

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        char ch[256];
        int cha[256];
        int count;
        printf("Enter a sentence:");
        fgets(ch,256,stdin);
        printf("\n%s",ch);
        for(count=0;ch[count]!='\0';count++)
        {
                                            cha[count]=((int)ch[count]+2);      
        }
        cha[count]=0;
        printf("\n After Encryption:  ");
            /*     for(count=0;cha[count]!=0;count++)
        {
                                          printf("%c",cha[count]);
        }*/
        
        puts((char)cha); /* [Warning] cast from pointer to integer of different size */
        getch();
        return 0;
    }
    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    What is this program supposed to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,749
    Print it the same way that you did the original.
    It seems to be a string ending in a nul.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by laserlight View Post
    What is this program supposed to do?
    Sorry.

    It is supposed to take input string and then make it a jumbled secret code. It should add 2 to the ASCII value of each character.

    Like A(=97) becomes B (=98).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Okay, so I'm guessing that the reason that you dont want to just do a printf() like earlier in the code is that you want to display the ASCII values of the "encrypted" characters?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by Salem View Post
    Print it the same way that you did the original.
    It seems to be a string ending in a nul.
    Yes, it is printing a special character (A circle with an arrow pointing downwards) after the encrypted string. But it was doing the same with printf so i commented it out and used puts, thought it would be better to use puts.

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    you want to display the ASCII values of the "encrypted" characters?
    No I want to display the characters, not their ASCII value.

  8. #8
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    If I input ABC it should give CDE.

    But it is giving a special character (A circle with an arrow pointing downwards) after the E.
    Last edited by abh!shek; 02-02-2008 at 01:36 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Ah, that is much clearer now.

    Basically, you only need one string. You do not need an array of ints. Then you can just write:
    Code:
    for (count = 0; cha[count] != '\0' && cha[count] != '\n'; count++)
    {
        cha[count] += 2;
    }
    cha[count] = '\0';
    printf("\nAfter Encryption: &#37;s", cha);
    The catch is that if your string contains the characters '}' or '~', you will end up with unprintable characters.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,749
    > But it is giving a special character (A circle with an arrow pointing downwards) after the D.
    That would be your encrypted version of the \n at the end of the line (as returned by fgets)

    Edit:
    Beaten.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Yes its working now. Thanks!
    Last edited by abh!shek; 02-02-2008 at 01:16 AM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Quote Originally Posted by laserlight View Post
    Ah, that is much clearer now.

    Basically, you only need one string. You do not need an array of ints. Then you can just write:
    Code:
    for (count = 0; cha[count] != '\0' && cha[count] != '\n'; count++)
    {
        cha[count] += 2;
    }
    cha[count] = '\0';
    printf("\nAfter Encryption: &#37;s", cha);
    The catch is that if your string contains the characters '}' or '~', you will end up with unprintable characters.
    I think you were supposed to use ch[] not cha[]. And you can fix the problem of non-printable characters by putting an if statement:
    Code:
    for (count =0; ch[count] != '\0' && ch[count] != '\n'; count++)
    {
        if (ch[count] >= sizeof(char) - 2)
            ch[count] -= sizeof(char)-2;
        else
            ch[count] += 2;
    }
    Last edited by Abda92; 02-02-2008 at 05:21 AM.

  13. #13
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by laserlight View Post

    The catch is that if your string contains the characters '}' or '~', you will end up with unprintable characters.

    Why are } and ~ unprintable ?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Why are } and ~ unprintable ?
    They are printable, but when you add 2 to them, the result is not printable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by laserlight View Post
    They are printable, but when you add 2 to them, the result is not printable.
    I still don't get it.
    '}' has ASCII value 125.
    And 127 is a triangle. So it should print a triangle..

    ~ has value 126
    and 128 is C (not the regular C, it has slightly different shape), so it should print that C....

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Program to find Point in Rectangle
    By DMJKobam in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 09:56 PM
  2. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  3. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 10:01 AM
  4. program design question
    By theroguechemist in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2004, 08:45 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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