Skip to content

Instantly share code, notes, and snippets.

@y0t4
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y0t4/e37ab3ecb8143003b3d5 to your computer and use it in GitHub Desktop.
Save y0t4/e37ab3ecb8143003b3d5 to your computer and use it in GitHub Desktop.
てきとーに
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DIFF_UPPER_LOWER ('a'-'A');
void replace(char *dest, char *str)
{
int length = strlen(str);
for (int i=0; i < length; i++) {
if ( 'A'<=str[i] && str[i]<='Z' ) {
dest[i] = str[i] + DIFF_UPPER_LOWER;
} else if ( 'a'<=str[i] && str[i]<='z' ) {
dest[i] = str[i] - DIFF_UPPER_LOWER;
} else {
dest[i] = str[i];
}
}
}
void print_data(char *dest, char *str)
{
printf("str : %s\n", str);
printf("dest: %s\n", dest);
}
int main(int argc, char *argv[])
{
if (argc == 1) {
fprintf(stderr, "Usage: ./a.out [string] [string] ...\n");
exit(1);
}
int length;
char *str;
char *dest;
for (int i = 1; argv[i] != NULL; i++) {
length = strlen(argv[i])+1;
str = (char *)calloc(length, sizeof(char));
dest = (char *)calloc(length, sizeof(char));
strncpy(str, argv[i], length);
replace(dest, str);
print_data(dest, str);
free(str);
free(dest);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment