1

I'm trying to make a simple encryption program with C. My aim is to translate abc (it can be any word) to 123. Then multiply 2 and get 246 then again translate to text, then write on screen bdf. Here is my algorithm which is not working correctly. I entered abc and I got cbc. Can you help me?

int main()
{
    int z,o,c,l,i,j,k,*D;
    char word[10];
char alfabe[24]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','y','z','\0'};

    printf("enter word");
    scanf("%s",word);

    c=strlen(word);
    printf("has %d letters ", c);
    D = (int *) malloc( sizeof(int)*c ); 
    for(i=0;i<c;i++) {
        for(j=0;j<26;j++) {
            if(word[i]==alfabe[j]) {  
                 D[i]=2*(j+1);
                 break;
            }
        }
    }
     printf("\nlast form before translation ");
     for(l=0;l<c;l++) {
       printf("%d",D[l]);  /*it s just for control */

    }

    for(z=0;z<c;z++){
printf("%c",alfabe[o]);
                      o=D[z];
                      word[z]=alfabe[o] ; break; }   




    printf("\nnew form of word: ");
    for(k=0;k<c;k++) {
       printf("%c",word[k]);

    }
scanf("%d");


}
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
allstar
  • 109
  • 3
  • 12

3 Answers3

2

The problem is in the following loop.

for(z=0;z<c;z++){
    printf("%c",alfabe[o]);
    o=D[z];
    word[z]=alfabe[o] ; 
    break; 
}   

Why did you break? It just translates first character. Second, you need to subtract 1 to get the right index of alphabet array(to redo the addition you did).

 word[z]=alfabe[o-1];

Third, you are using o before initializing it? Your compiler should warn you for this. Fourth, why are you storing 27 characters in char array of size 24?

char alfabe[24]={'a','b',...........,'\0'}

And last but not least you need to use modular arithmetic, this wont work if user enters something like xyz.

vidit
  • 6,293
  • 3
  • 32
  • 50
  • thanks for your answer.I want to know something more.I define word i did char word[10] but i think it s not good solution how can i use malloc with strings.I used c=strlen(word); so should i write like that char *word char = (char *) malloc( sizeof(char)*c ); – allstar May 03 '12 at 23:10
1

OK, first of all '\0' marks the end of an inputed string, you don't need to encrypth this particular character, my suggestion is to place it first in the alfabet so you would get:

alfabet[24] = {'\0', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','y','z'};

This will save you the trouble of substracting 1, so you will have:

for (i = 0; i < c; i++)
{
    for (j = 0; j < 24; j++)
    {
        if (word[i] == alfabet[j])
        {  
            D[i] = 2 * j;
        }
    }
}

In the part where you encode the input. And when you generate the output word:

for (z = 0; z < c; z++)
{
    printf("%c", alfabet[D[z]]);
    word[z] = alfabet[D[z]];
}

No need for o and especially no break; in the loop.

A more efficient way would be to create a function that handles the encryption of the string passed:

char* getEncryptedWord(char* word)
{
    char alfabet[25]={'\0', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v', 'x', 'y','z'};

    int i, j;
    int size = strlen(word);

    char* encryptedWord = (char*) malloc(size + 1);

    for (i = 0; i < size; i++)
    {
            for (j = 0; j <= 25; j++)
            {
                    if (word[i] == alfabet[j])
                    {
                            if (2 * j > 25)
                                    encryptedWord[i] = alfabet[2 * j % 25];
                            else
                                    encryptedWord[i] = alfabet[2 * j];

                            break;
                    }
            }
    }

    encryptedWord[size] = '\0';

    return encryptedWord;
}

I've added 'x' in the alfabet - that is the reason why now there are 25 elements. I'm sure there is one character of the English alphabet missing, but it's 3 AM here and English isn't my primary language. Also, this solution is working on the assumption that the alfabet you provided are the only characters that are supposed to exist in the input and output.

George
  • 307
  • 3
  • 10
  • thanks for your answer.I want to know something more.I define word i did char word[10] but i think it s not good solution how can i use malloc with strings.I used c=strlen(word); so should i write like that char *word char = (char *) malloc( sizeof(char)*c ); – allstar May 03 '12 at 23:10
  • Thanks vidit for proving wrong the first answer I gave... I just wrote the code blindly, I didn't test it... – George May 03 '12 at 23:39
  • allstar - fist off, you still need to read into `word` the input string. There is [question](http://stackoverflow.com/questions/8164000/how-to-dynamically-allocate-memory-space-for-a-string-and-get-that-string-from-u) on this site that provides an answer for that. If you want to allocate a new `word2` then `char* word2 = (char*) malloc(strlen(word))` should do it. – George May 03 '12 at 23:44
0

(tip: If you only expect letters A-Z, you don't need to loop through the array to find the corresponding number, you may simply get the number by subtracting the numerical value of 'a', and add 1, if you want the letter 'a' to map to 1.)

Modulo arithmetic is mentioned, but that will give you problems because you will loose the 1:1-mapping, i.e. two letters can end up being translated to the same number.

daniero
  • 335
  • 1
  • 8
  • 11