Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
51
Task/Rot-13/C/rot-13.c
Normal file
51
Task/Rot-13/C/rot-13.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static char rot13_table[UCHAR_MAX + 1];
|
||||
|
||||
static void init_rot13_table(void) {
|
||||
static const unsigned char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
static const unsigned char lower[] = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
for (int ch = '\0'; ch <= UCHAR_MAX; ch++) {
|
||||
rot13_table[ch] = ch;
|
||||
}
|
||||
for (const unsigned char *p = upper; p[13] != '\0'; p++) {
|
||||
rot13_table[p[0]] = p[13];
|
||||
rot13_table[p[13]] = p[0];
|
||||
}
|
||||
for (const unsigned char *p = lower; p[13] != '\0'; p++) {
|
||||
rot13_table[p[0]] = p[13];
|
||||
rot13_table[p[13]] = p[0];
|
||||
}
|
||||
}
|
||||
|
||||
static void rot13_file(FILE *fp)
|
||||
{
|
||||
int ch;
|
||||
while ((ch = fgetc(fp)) != EOF) {
|
||||
fputc(rot13_table[ch], stdout);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
init_rot13_table();
|
||||
|
||||
if (argc > 1) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
FILE *fp = fopen(argv[i], "r");
|
||||
if (fp == NULL) {
|
||||
perror(argv[i]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
rot13_file(fp);
|
||||
fclose(fp);
|
||||
}
|
||||
} else {
|
||||
rot13_file(stdin);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue