// 大文字と小文字の反転処理
// -英半角大文字は英半角小文字に変換し、英半角小文字は英半角大文字に変換する
// -漢字(2バイト文字)の場合は、そのまま
#include <stdio.h>
#include <ctype.h>
/*
半角英大文字→半角英小文字
半角英小文字→半角英大文字
に変換するツール
*/
// 漢字の判定
int iskanji (char c)
{
if(((0x81 <= (unsigned char)(c) && (unsigned char)(c) <= 0x9F)
|| (0xE0 <= (unsigned char)(c) && (unsigned char)(c) <= 0xEF))) {
return 1;
} else {
return 0;
}
}
int main (int argc, char *argv[])
{
FILE *inputfp;
FILE *outputfp;
char text[1024];
char str[1024];
int i;
if (argc != 3) {
printf ("引数が不正です\n");
return -1;
}
inputfp = fopen (argv[1], "r");
if (inputfp == NULL) {
printf("入力ファイルのオープンに失敗しました\n");
return -1;
}
outputfp = fopen (argv[2], "w");
if (outputfp == NULL) {
printf("出力ファイルのオープンに失敗しました\n");
return -1;
}
while (fgets (text, sizeof(text), inputfp) != NULL) {
// 漢字判定
for (i = 0;text[i] != '\0';i++) {
if (iskanji (text[i]) == 1) {
str[i] = text[i]; // 1バイト目
i++;
str[i] = text[i]; // 2バイト目
continue;
}
if (isalpha(text[i]) != 0) {
if (islower(text[i]) != 0) {
str[i] = toupper(text[i]);
} else {
str[i] = tolower(text[i]);
}
} else {
str[i] = text[i];
}
}
str[i] = '\0';
if (fputs (str, outputfp) == -1) {
printf ("ファイルの書き込みに失敗しました\n");
return -1;
}
}
fclose (inputfp);
if (fclose (outputfp) == EOF) {
printf ("アウトプットファイルのクローズに失敗しました\n");
return -1;
}
return 0;
}
最終更新:2008年10月26日 21:06