ROT13 in C
Atsutane, 09.09.2008 - 20:30
Ich habe gerade für einen Bekannten ein kleines C-Beispiel geschrieben, wie man recht mühelos ROT13 in C bettet, für den ein oder anderen Programmierneuling bestimmt interessant.
main.c
#include <stdio.h>
#include <stdlib.h>
#include "rot13.h"
int main(void)
{
char text[] = "Dieser Text soll verschluesselt werden.";
printf("%s\n",text);
rot13_crypt(text);
printf("%s\n", text);
rot13_crypt(text);
printf("%s\n", text);
return 0;
}
rot13.h
#include <stdio.h>
#include <stdlib.h>
#define TABLE_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM"
#define TABLE_LOWER "abcdefghijklmnopqrstuvwxyzabcdefghijklm"
void rot13_crypt(char *);
char rot13_lower(char);
char rot13_upper(char);
rot13.c
#include <stdio.h>
#include <stdlib.h>
#include "rot13.h"
void rot13_crypt(char *text)
{
int i=0;
while (text[i] != 0)
{
text[i] = rot13_lower(text[i]);
text[i] = rot13_upper(text[i]);
i++;
}
}
char rot13_lower(char c)
{
int i = 0;
char table[] = TABLE_LOWER;
while (table[i] != 0)
{
if (c == table[i])
return table[i+13];
i++;
}
return c;
}
char rot13_upper(char c)
{
int i = 0;
char table[] = TABLE_UPPER;
while (table[i] != 0)
{
if (c == table[i])
return table[i+13];
i++;
}
return c;
}
Stilistisch u.U. nicht ganz einwandfrei, wer möchte, darf in den Kommentaren meckern ![]()
Nachtrag 11.9.08: Ich wurde von PSychoPath_ wegen der Lizenz gefragt, da hier eigentlich nichts wirklich anspruchvolles vorliegt, kommt für den Code Public Domain zum Einsatz


Kommentare:
anarchopunk - 13.09.2008 - 14:12
Jetzt noch C können, dann würd mir des sogar was bringen ;D Aber dieses Schuljahr machen wir kleines bisschen, wegen dem NXT mit dem wir zu nem Wettbewerb wollen (bzw unser Rektor..)
BadBoy_s Home - 13.09.2008 - 17:43
ROT13 in Ruby
(...) Bei Atsutane habe ich ein kleines Beispiel einer Implementierung von ROT13 in C gesehen.
Dank des Wikipedia-Artikels bin ich da (...)
Hinterlasse selbst einen Kommentar: