🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#15245
From: Charlie Gordon
Date: Mon, 03 Oct 2005 02:26
84 lines
3824 bytes
"Richard Delorme" <abulmo@nospam.fr> wrote in message
news:43403d27$0$306$7a628cd7@news.club-internet.fr...
> Harpo a �crit :
> >
> > 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) {

pas besoin d'une variable init s�par�e, on peut tester crypt[1] qui vaut 0 avant
initialisation et 1 ou une lettre de l'alphabet (donc pas 0) apr�s.

>        char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
>        char rot13_alphabet[26] = "nopqrstuvwxyzabcdefghijklm";

�viter les initialisations de tableaux automatiques: utiliser des const char *
ou des tableaux static.

>        unsigned int i;

pourquoi pas int ?

>        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];

le cast en (int) de alphabet[i] est inutile, il ne fait rien du tout, de plus
alphabet[i] est forc�ment positif, donc inutile de caster en (unsigned char).

On peut aussi se passer de rot13_alphabet en �crivant:

crypt[alphabet[i]] = alphabet[(i + 13) % 26];

Mais c'est l�g�rement plus co�teux sur les rares machines o� la division est
lente.

>           crypt[toupper(alphabet[i])] = toupper(rot13_alphabet[i]);
>        }
>     }
>
>     for (;*s; ++s) {
>        *s = crypt[(int)((unsigned char) *s)];

*s = crypt[(unsigned char)(*s)];

c'est plus simple (on peut meme supprimer les ())

>     }
> }
>
> int main (void)
> {
>     char s[] = "La vie est un long fleuve tranquille!";
>
>     puts(s);
>     rot13(s);
>     puts(s);
>
>      return 0;
> }
> /*---------8<----------------*/

-- 
Chqrlie.
Technical Details
Message-ID: <dhq3bs$dbc$1@reader1.imaginet.fr>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!elnk-atl-nf1!newsfeed.earthlink.net!nx01.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!fr.colt.net!writer!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> <43403d27$0$306$7a628cd7@news.club-internet.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!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!elnk-atl-nf1!newsfeed.earthlink.net!nx01.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!fr.colt.net!writer!not-for-mail
From: "Charlie Gordon" <news@chqrlie.org>
Newsgroups: fr.comp.lang.c
Subject: Re: rot13 [Was : Re: conjugaison et autres ...]
Date: Mon, 3 Oct 2005 04:26:15 +0200
Organization: ImagiNET / Colt Internet
Lines: 87
Message-ID: <dhq3bs$dbc$1@reader1.imaginet.fr>
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> <43403d27$0$306$7a628cd7@news.club-internet.fr>
NNTP-Posting-Host: gw.netgem.com
X-Trace: reader1.imaginet.fr 1128304828 13676 195.68.2.34 (3 Oct 2005 02:00:28 GMT)
X-Complaints-To: abuse@imaginet.fr
NNTP-Posting-Date: 3 Oct 2005 02:00:28 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506