Ok, before I startlet me describe what the program that i'm writing needs to do..
1. The program asks the user to enter a 10-letter word.
2. Each letter/character entered, is encrypted by changing it to the next adjacent letter of the alphabet.
eg. if 'a' is entered, it is changed to 'b'
eg, if the word "dumb" is entered, it is changed to "evnc"
Now, I've written a code to do this, but it doesn't seem to work well. It is returning the word that i'm inputting same to same
(i.e, no encryption is taking place.)
here is my code:
Code:
# define n 26
int main()
{
char a[10];
char b[n]={'a', 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int i,j;
printf("Please type your sentence to be Encrypted below: \n\n");
for(i=0; i<10; i++)
{
scanf("%c", &a[i]);
}
for(i=0; i<10; i++)
{
printf("%c", a[i]);
}
for(j=0; j<n; j++)
for (i=0; i<10; i++)
{
if (a[i]!=b[j])
{
j++; // if there is no match, increment j and continue comparing
}
else //after a match is found, assign the content at b[j+1] to a[i]
{ // then print a[i]
a[i]=b[j+1];
printf("%c", a[i]);
i++;
}
}
return 0;
}
Whats wrong with this code?
If, this is logically not good, do you know any better way of doing it??
If so, suggest me so that I can write the program and make it generated the correct output..
Any help/suggestion is greatly appreciated.