R
Rob Morris
Hi, I'm teaching myself C for fun. I wrote the litle program listed
below to convert rot13 text. It reads one char at a time and converts
it via pointers.
The constant char* letters holds the alphabet. I subract the pointer
returned from strchr from the address of letters to get the location
within the alphabet, then rot13 it. My question is, is this safe and
legal? (It works on my windows machine BTW.) I googled for programs to
do this and they mostly just subtracted 'a' from the input, which I
gather is ASCII only - so is my program any more portable?
Would you recommed another method for things like this?
Many thanks,
Rob Morris
#include <stdio.h>
#include <string.h>
int main (void)
{
int in, out;
char *loc;
char *letters="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
while ((in = getchar()) != EOF) {
loc = strchr(letters, in);
if (loc != NULL)
out = *(((loc-letters+26)%52)+letters);
else
out = in;
putchar(out);
}
return 0;
}
below to convert rot13 text. It reads one char at a time and converts
it via pointers.
The constant char* letters holds the alphabet. I subract the pointer
returned from strchr from the address of letters to get the location
within the alphabet, then rot13 it. My question is, is this safe and
legal? (It works on my windows machine BTW.) I googled for programs to
do this and they mostly just subtracted 'a' from the input, which I
gather is ASCII only - so is my program any more portable?
Would you recommed another method for things like this?
Many thanks,
Rob Morris
#include <stdio.h>
#include <string.h>
int main (void)
{
int in, out;
char *loc;
char *letters="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
while ((in = getchar()) != EOF) {
loc = strchr(letters, in);
if (loc != NULL)
out = *(((loc-letters+26)%52)+letters);
else
out = in;
putchar(out);
}
return 0;
}