|
>質問です。Jon-P.Sato.の場合、a〜zA〜Zで人文字を付加すると、
>Jon-Q.Sato.になってしまいます。Jon-P-a.Sato.にするための
>判別条件は何でしょうか。
上記にご返信がないので、Jon-Q.Sato.になりますが、次のような
プログラムを作ってみました。
条件漏れなどあるかもしれませんが、upします。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 6
int alpncmp(const char str1[], const char str2[], int len);
int main(void)
{
char str_nm[N][41] = {"Taro.Yamada.", "Hanako.Yamamoto.",
"Hanako-a.Yamamoto.", "Hanako-b.Yamamoto.",
"Jon-P.Sato.", "Lu-Will.Lu."};
char *p1, *p2, *maxp, maxstr[N][41];
int i, j, k = 0, m, len1, len2;
for (i = 0; i < N; i++) {
m = i; /* i番目を仮にMAXとする */
p1 = str_nm[i];
len1 = strcspn(p1, "."); /* .までの長さを取得 */
if (*(p1+len1-2) == '-' && isalpha(*(p1+len1-1)) != 0)
len1 -= 2; /* -X の長さは含めない */
/*** 既にMAXを見つけたか調べる ***/
for (j = 0; j < k; j++) {
if (strncmp(maxstr[j], p1, len1) == 0) break;
}
if (j != k) continue; /* 既にあれば次へ */
/*** MAXを見つける ***/
maxp = maxstr[k];
*maxp = '\0';
strncat(maxp, p1, len1); /* -X を含めない文字列をmaxstrに設定 */
for (j = i; j < N; j++) {
p2 = str_nm[j];
len2 = strcspn(p2, "."); /* .までの長さを取得 */
if (strncmp(maxp, p2, len1) == 0) { /* -X までが一致したら */
if (alpncmp(maxp, p2, len2) < 0) { /* maxstrより大きいときは */
m = j; /* MAXを書き換える */
*maxp = '\0';
strncat(maxp, p2, len2);
}
}
}
/*** MAXの更新 ***/
len1 = strlen(maxp);
if (*(maxp+len1-2) == '-') {
if (*(maxp+len1-1) == 'z')
*(maxp+len1-1) = 'A';
else if (*(maxp+len1-1) == 'Z')
printf("表現できる最大値を超えました。\n");
else if (isalpha(*(maxp+len1-1)) != 0)
*(maxp+len1-1) += 1;
else
strcat(maxp, "-a");
}
else
strcat(maxp, "-a");
strcat(maxp, &str_nm[m][len1]);
printf("%s\n", maxp);
k++;
}
return 0;
}
/* a〜zA〜Zの大小比較 */
int alpncmp(const char str1[], const char str2[], int len)
{
int i, c1, c2;
for (i = 0; i < len; i++) {
if (str1[i] != str2[i]) {
/* 大文字と小文字のコードを逆転させる */
if (islower(str1[i]) != 0)
c1 = str1[i] - 'a' + 'A';
else if(isupper(str1[i]) != 0)
c1 = str1[i] - 'A' + 'a';
if (islower(str2[i]) != 0)
c2 = str2[i] - 'a' + 'A';
else if(isupper(str2[i]) != 0)
c2 = str2[i] - 'A' + 'a';
return (c1 - c2);
}
if (str1[i] == '\0' && str2[i] == '\0') break;
}
return 0;
}
|