🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fr.comp.lang.c
Article #15232

Re: rot13 [Was : Re: conjugaison et autres ...]

#15232
From: Richard Delorme
Date: Sun, 02 Oct 2005 20:03
62 lines
3033 bytes
Harpo a �crit :
> Emmanuel Delahaye wrote:
> 
> 
>>Lid�e est de cr�er 2 tables avec tous les caract�res (A-Z) et (a-z),
>>et de chercher dedans la position 'i' du caract�re avec strchr().
>>Ensuite, on va chercher le caract�re � (i + 13) % 26...
> 
> 
> Je pense qu'il y a plus simple et plus performant :
> 
> On prend une table dont la taille correspond aux valeurs possibles des
> caract�res ( g�n�ralement 256 ), � l'emplacement dont l'offset
> correspond � la valeur d'un caract�re, on place le caract�re par lequel
> il doit �tre traduit.

Je crois qu'il y a quelque subtilit� si l'on veut tenir compte du signe 
du type char, et avoir un truc portable. �a pourrait ressembler � :

/*---------8<----------------*/
#include <stdio.h>
#include <limits.h>
#include <ctype.h>

void rot13(char s[])
{
    static int init = 0;
    static char crypt[UCHAR_MAX];

    if (!init) {
       char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
       char rot13_alphabet[26] = "nopqrstuvwxyzabcdefghijklm";
       unsigned int i;
       init = 1;
       for (i = 0; i <= UCHAR_MAX; ++i) {
          crypt[i] = i;
       }
       for (i = 0; i < 26; ++i) {
          crypt[(int) alphabet[i]] = rot13_alphabet[i];
          crypt[toupper(alphabet[i])] = toupper(rot13_alphabet[i]);
       }
    }

    for (;*s; ++s) {
       *s = crypt[(int)((unsigned char) *s)];
    }
}

int main (void)
{
    char s[] = "La vie est un long fleuve tranquille!";

    puts(s);
    rot13(s);
    puts(s);

     return 0;
}
/*---------8<----------------*/

-- 
Richard
Technical Details
Message-ID: <43403d27$0$306$7a628cd7@news.club-internet.fr>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!proxad.net!m.t-online.fr!v.t-online.fr!t-online.fr!not-for-mail
References: <1126342327.583568.21420@g43g2000cwa.googlegroups.com> <4329e5d4$0$7846$8fcfb975@news.wanadoo.fr> <dge4b6$fqa$1@reader1.imaginet.fr> <432ab809$0$1734$8fcfb975@news.wanadoo.fr> <mn.84b07d59016a857b.31483@laposte.net> <433f16ac$0$27406$8fcfb975@news.wanadoo.fr> <mn.10907d5a55119e84.15512@YOURBRAnoos.fr> <433f9cb1$0$7821$8fcfb975@news.wanadoo.fr> <mn.128c7d5aa5238de0.15512@YOURBRAnoos.fr> <43403203$0$5388$8fcfb975@news.wanadoo.fr>

Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,b21b26167a936e28
X-Google-Attributes: gidf9822,public
X-Google-Language: FRENCH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!proxad.net!m.t-online.fr!v.t-online.fr!t-online.fr!not-for-mail
Date: Sun, 02 Oct 2005 22:03:45 +0200
From: Richard Delorme <abulmo@nospam.fr>
User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050801)
X-Accept-Language: fr, en
MIME-Version: 1.0
Newsgroups: fr.comp.lang.c
Subject: Re: rot13 [Was : Re: conjugaison et autres ...]
References: <1126342327.583568.21420@g43g2000cwa.googlegroups.com> <4329e5d4$0$7846$8fcfb975@news.wanadoo.fr> <dge4b6$fqa$1@reader1.imaginet.fr> <432ab809$0$1734$8fcfb975@news.wanadoo.fr> <mn.84b07d59016a857b.31483@laposte.net> <433f16ac$0$27406$8fcfb975@news.wanadoo.fr> <mn.10907d5a55119e84.15512@YOURBRAnoos.fr> <433f9cb1$0$7821$8fcfb975@news.wanadoo.fr> <mn.128c7d5aa5238de0.15512@YOURBRAnoos.fr> <43403203$0$5388$8fcfb975@news.wanadoo.fr>
In-Reply-To: <43403203$0$5388$8fcfb975@news.wanadoo.fr>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 62
Message-ID: <43403d27$0$306$7a628cd7@news.club-internet.fr>
Organization: Club-Internet / T-Online France
NNTP-Posting-Date: 02 Oct 2005 20:03:54 GMT
NNTP-Posting-Host: 62.34.128.94
X-Trace: 1128283434 news.club-internet.fr 306 62.34.128.94
X-Complaints-To: abuse@club-internet.fr