C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 28th, 2012, 03:11 PM
mnkjoi mnkjoi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 mnkjoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m
Reputation 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 ? ..... Any ideas please

Reply With Quote
  #2  
Old November 28th, 2012, 03:36 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,813 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 7 h 50 m 44 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
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

Reply With Quote
  #3  
Old November 28th, 2012, 03:57 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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?

Quote:
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.

Reply With Quote
  #4  
Old November 29th, 2012, 12:37 PM
mnkjoi mnkjoi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 mnkjoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m
Reputation Power: 0
Thanks all for reply .... But how can I solve this issue ? I tried but couldnt solve it

Reply With Quote
  #5  
Old November 29th, 2012, 01:04 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help with my code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap