i already did a program that encrypts letters by skipping 1 or 2. but now i need to make a program that encrypts a word, using substitution. I already try several things but i can't get it work. can anybody help me?

for example the normal key is a,b,c,d,e
and the secret key is f,g,h,i,j.

the program prompts the user to enter a word using the letters in the secret key. and then outputs the word using the normal key.

for example: enter the secret key: f,g,h,i,j
enter a word in the secret key: ggijf
your word is: bbda

code:

#include <stdio.h>

int main()
{
   char abracadabra[] = {'a','b','c','d','e'}
   char secret_key[] = {'f','g','h','i','j'}
   char new_word[] = {"ggijf"}

   for(i=0; i<5; i++)
   {
       secret_key[i] = abracadabra[i];
       new_word[i] = secret_key[i];
    }

   for(i-0; i<5; i++)  
   printf("your word is: %c", new_word[i]);

return 0;

}

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

Comments
[code] tag. Learn to use them.
22 posts, and you still haven't figured out code tags.

i already did a program that encrypts letters by skipping 1 or 2. but now i need to make a program that encrypts a word, using substitution. I already try several things but i can't get it work. can anybody help me?

for example the normal key is a,b,c,d,e
and the secret key is f,g,h,i,j.

the program prompts the user to enter a word using the letters in the secret key. and then outputs the word using the normal key.

for example: enter the secret key: f,g,h,i,j
enter a word in the secret key: ggijf
your word is: bbda

code:

#include <stdio.h>

int main()
{
char abracadabra[] = {'a','b','c','d','e'}
char secret_key[] = {'f','g','h','i','j'}
char new_word[] = {"ggijf"}

for(i=0; i<5; i++)
{
secret_key = abracadabra;
new_word = secret_key;
}

for(i-0; i<5; i++)
printf("your word is: %c", new_word);

return 0;

}

What you need to do is locate the position of each input char in the secret string and replace it with the char that exists at that position in the abracadabra string.

#include <stdio.h>
#include <string.h>

int main() {
   char abracadabra[] = {'a','b','c','d','e'};
   char secret_key[] = {'f','g','h','i','j'};
   char new_word[] = {"ggifj"};
   char *pos;
   int i;
   
   for (i=0; i<5; i++) {
      pos = strchr(secret_key, new_word[i]);
      new_word[i] = abracadabra[pos];
   }
   
   printf("Your new word is : %s",new_word);
}

Sorry I don't have access to a compiler here.
I am sure there is a bug or two in this code, but you should get the idea.

Comments
And here is negative rep for doing someone's homework for them

Can't seem to locate the edit post option, sorry I am a noob poster here..
Anyhow, here is the code again after I fixed a few small bugs.

#include <stdio.h>
#include <string.h>

int main() {
  char abracadabra[] = {'a','b','c','d','e'};
  char secret_key[] = {'f','g','h','i','j'};
  char new_word[] = {"ggifj"};
  char *pos;
  int i;

  for (i=0; i<5; i++) {
    pos = strchr(secret_key, new_word[i]);
    new_word[i] = abracadabra[(int)(pos-secret_key)];
  }

  printf("Your new word is : %s\n",new_word);
}
This article has been dead for over six months. Start a new discussion instead.