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;

}
Salem commented: 22 posts, and you still haven't figured out code tags. -4
kvprajapati commented: [code] tag. Learn to use them. -1

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.

commented: And here is negative rep for doing someone's homework for them -2

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.