The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Help with my code
Discuss Help with my code in the C Programming forum on Dev Shed. Help with my code C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 28th, 2012, 03:11 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
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
|

November 28th, 2012, 03:36 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
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
|

November 28th, 2012, 03:57 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

November 29th, 2012, 12:37 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
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
|

November 29th, 2012, 01:04 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|