単純な解決策は、おそらく一度に 1 文字を読み取り、それが , のときに'U'
別の文字を読み取って'I'
、それがグループ内の次の文字かどうかを確認することです'N'
。'K'
そうであれば、そのグループのカウンターを増やします。他のすべての文字は単純に破棄されます。
編集:関数の例:
int count_uu = 0;
int count_ii = 0;
int count_nj = 0;
int count_ku = 0;
void check_next_char(int expected, FILE *input, int *counter);
void count(FILE *input)
{
int ch; /* Character we read into */
while ((ch = fgetc(input)) != EOF)
{
switch (ch)
{
case 'U':
check_next_char('U', input, &count_uu);
break;
case 'I':
check_next_char('I', input, &count_ii);
break;
case 'N':
check_next_char('J', input, &count_nj);
break;
case 'K':
check_next_char('U', input, &count_ku);
break;
default:
/* Not a character we're interested in */
break;
}
}
/* This function gets the next character from a file and checks against
an `expected` character. If it is same as the expected character then
increase a counter, else put the character back into the stream buffer */
void check_next_char(int expected, FILE *input, int *counter)
{
int ch = fgetc(input);
if (ch == expected)
(*counter)++;
else
ungetc(ch, input);
}