allstar
allstar

Reputation: 109

To change letter of word/C

I m trying to write Encryption Algorithm.I change order of alphabet.

char alfabe[26]={'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'}; 

after changing i want to assign last to letter z and y but i couldnt to with these codes.

 strcpy(alfabe[25],"z");
         strcpy(alfabe[26],"y");

Upvotes: 0

Views: 673

Answers (4)

user267885
user267885

Reputation:

You can do a simple transposition (Caesar cypher) mechanism more easily without manually defining an alphabet.

char* text;
int i, num_transpose;

for(i=0; a[i] != '\0'; i++)  
  if (a[i] >= 'a' && a[i] <= 'z')
    text[i] = (text[i] - 'a' + num_transpose) % ('z'-'a'+1) + 'a';

That would shift letters in your text by num_transpose places in alphabet. Note though that this will only work for lower-case ASCII letters.

Upvotes: 0

jjrv
jjrv

Reputation: 4345

Maybe clearest would be to write:

alfabe['y' - 'a'] = 'z';
alfabe['z' - 'a'] = 'y';

This makes it fairly obvious that you're swapping y and z in the table.

Upvotes: 0

Michael Slade
Michael Slade

Reputation: 13877

This will work for lower case letters at least:

char *p;
for(p = mystr; *p; p++)
    if(*p >= 'a' && *p <= 'z')
        *p = alfabe[*p-'a'];

But remember, transposition is not encryption!

Upvotes: 1

William Pursell
William Pursell

Reputation: 212504

alfabe[24] = 'z';
alfabe[25] = 'y';

strcpy is absolutely the wrong thing to use here.

Upvotes: 0

Related Questions