#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Help with my code


    Hello

    I'm trying to type code that encrypt letters and here is my code

    #include<stdio.h>

    int main()
    {
    char word [20];
    int i,j,n;
    char orig[27][2]={{'A','H'},{'B','F'},{'C','L'},{'D','R'},
    {'E','U'},{'F','O'},{'G','P'},{'H','C'},
    {'I','B'},{'J','A'},{'K','Z'},{'L','E'},
    {'M','W'},{'N','D'},{'O','G'},{'P','Y'},
    {'Q','I'},{'R','M'},{'S','K'},{'T','N'},
    {'U','X'},{'V','S'},{'W','J'},{'X','V'},
    {'Y','Q'},{'Z',' '},{' ','V'},};

    for (i=0;i<27;i++){
    for (j=0;j<2;j++)
    printf ("%c\t",orig[i][j]);
    printf ("\n");
    }
    printf ("Enter your word to be encrypted\n");
    for (n=0;n<20;n++){
    scanf ("%c",&word[n]);
    if (word[n]=='\n')
    break;
    }
    for (i=0;i<n;i++){
    for (j=0;j<2;j++){
    if (word[i]==orig[i][0]){
    word[i]=orig[i][1];
    }
    }
    printf ("%c ",word[i]);
    }
    return 0;
    }


    when I type A and I hit enter button the programs encrypts it with H. But if I type any letter the program prints for me the same letter WHY ? :confused: :confused: ..... Any ideas please
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Code:
    for (i=0;i<n;i++){
    for (j=0;j<2;j++){
    if (word[i]==orig[i][0]){
    word[i]=orig[i][1];
    }
    }
    Notice if your string just has one character, i will only have the value 0, so only if the character is 'A' will you get the character it should be.

    Try entering AB
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Use code tags! This isn't your first post here, so you should already know better!

    Without code tags, your code loses all its formatting and becomes an unreadable mess.

    Here is your code with code tags:
    Code:
    #include<stdio.h>
    
    int main()
    {
        char word [20];
        int i,j,n;
        char orig[27][2]={{'A','H'},{'B','F'},{'C','L'},{'D','R'},
                         {'E','U'},{'F','O'},{'G','P'},{'H','C'},
                         {'I','B'},{'J','A'},{'K','Z'},{'L','E'},
                         {'M','W'},{'N','D'},{'O','G'},{'P','Y'},
                         {'Q','I'},{'R','M'},{'S','K'},{'T','N'},
                         {'U','X'},{'V','S'},{'W','J'},{'X','V'},
                         {'Y','Q'},{'Z',' '},{' ','V'},};
                         
                         for (i=0;i<27;i++){
                             for (j=0;j<2;j++)
                             printf ("%c\t",orig[i][j]);
                             printf ("\n");
                             }
                   printf ("Enter your word to be encrypted\n");
                   for (n=0;n<20;n++){
                       scanf ("%c",&word[n]);
                       if (word[n]=='\n')
                       break;
                       }
                       for (i=0;i<n;i++){
                            for (j=0;j<2;j++){
                            if (word[i]==orig[i][0]){
                                 word[i]=orig[i][1];                            
                                 }
                                 }
                                printf ("%c ",word[i]);                               
                           }
    return 0;
    }
    Now you can see your original formatting, which is atrocious. If you're going to attempt to use K&R open-brace placement (ie, hiding them at the ends of lines), then you absolutely must use clear and consistent indenting!

    Here is your code cleaned up, so that others will have a chance of being able to read it:
    Code:
    #include<stdio.h>
    
    int main()
    {
        char word [20];
        int i,j,n;
        char orig[27][2]={{'A','H'},{'B','F'},{'C','L'},{'D','R'},
                         {'E','U'},{'F','O'},{'G','P'},{'H','C'},
                         {'I','B'},{'J','A'},{'K','Z'},{'L','E'},
                         {'M','W'},{'N','D'},{'O','G'},{'P','Y'},
                         {'Q','I'},{'R','M'},{'S','K'},{'T','N'},
                         {'U','X'},{'V','S'},{'W','J'},{'X','V'},
                         {'Y','Q'},{'Z',' '},{' ','V'},};
                         
        for (i=0;i<27;i++)
        {
            for (j=0;j<2;j++)
                printf ("%c\t",orig[i][j]);
            printf ("\n");
        }
        
        printf ("Enter your word to be encrypted\n");
        for (n=0;n<20;n++)
        {
            scanf ("%c",&word[n]);
            if (word[n]=='\n')
                break;
        }
        
        for (i=0;i<n;i++)
        {
            for (j=0;j<2;j++)
            {
                if (word[i]==orig[i][0])
                {
                    word[i]=orig[i][1];                            
                }
            }
            printf ("%c ",word[i]);                               
        }
        
        return 0;
    }
    See what an incredible difference is made by doing it correctly?

    Originally Posted by mnkjoi
    when I type A and I hit enter button the programs encrypts it with H. But if I type any letter the program prints for me the same letter WHY ?
    In this loop, where you appear to be attempting to perform the encryption:
    Code:
        for (i=0;i<n;i++)
        {
            for (j=0;j<2;j++)
            {
                if (word[i]==orig[i][0])
                {
                    word[i]=orig[i][1];                            
                }
            }
            printf ("%c ",word[i]);                               
        }
    What is the purpose of j? As it stands, you are only testing the n input characters against the first n entries in your encryption table. Furthermore, you are specifically testing whether the first input character matches with the first table element, the second with the second, and so on. That worked for 'A', but no other input character will match with the 'A' element in the table, so they will remain unchanged.

    I think you want the orig[] index to be j and I think you would want to have j interate from 0 to 26.

    PS
    Once you have found a match, you might want to get out of that j loop.
    Last edited by dwise1_aol; November 29th, 2012 at 01:02 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Thanks all for reply .... But how can I solve this issue ? I tried but couldnt solve it
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Re-read my response (#3), especially the question about the for-j-loop in the encryption section. And also don't ignore the PS this time as well.

    Once I had made both corrections, it worked fine for me.

IMN logo majestic logo threadwatch logo seochat tools logo