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");


}
3
  • 4
    There are 26 alphabets in English plus a '\0', why a char array of 24? Commented May 3, 2012 at 22:37
  • You are printing alfeabe[o] before o is initialized, and you should be reversing the "encryption" by dividing D[z] by two and subtracting one somewhere. Commented May 3, 2012 at 22:45
  • 2
    You q-ist and w-ist, how dare you be so rude? And don't get me started about the x.... Commented May 3, 2012 at 23:00

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 );
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.

3 Comments

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 );
Thanks vidit for proving wrong the first answer I gave... I just wrote the code blindly, I didn't test it...
allstar - fist off, you still need to read into word the input string. There is question 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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.