■ 文字列の置換(平成17年春期午後問2)
#include <stdio.h>
/* 基本情報 平成17年春期午後問2 */
/* 文字列の置換 */
#define EOS '\0'
void Replace(char A[], char S[], char D[], char B[])
{
int Aidx, Sidx, Didx, Bidx, Idx;
Aidx = 0;
Bidx = 0;
while (A[Aidx] != EOS) {
if (A[Aidx] == S[0]) {
Idx = Aidx;
Sidx = 0;
do {
Sidx++;
Aidx++;
} while (A[Aidx] == S[Sidx] && A[Aidx] != EOS);
if (S[Sidx] == EOS) {
Didx = 0;
while (D[Didx] != EOS) {
B[Bidx] = D[Didx];
Didx++;
Bidx++;
}
} else {
B[Bidx] = S[0];
Aidx = Idx + 1;
Bidx++;
}
} else {
B[Bidx] = A[Aidx];
Aidx++;
Bidx++;
}
}
}
int main()
{
char A[10] = "aabcabb";
char S[10] = "ab";
char D[10] = "ABC";
char B[10];
Replace(A, S, D, B);
printf("A[]:%s\n", A);
printf("S[]:%s\n", S);
printf("D[]:%s\n", D);
printf("B[]:%s\n", B);
return 0;
}