Reputation: 894
Let's say i have a file filled with random characters with whitespaces and \n included also random.
I want to look for this groups of chars, example: UU, II, NJ, KU. So the purpose is to read the file, look for this kind of groups and say how many are they in the file.
My problem is whitespace and \n, becase if i find one of these i should skip it and search again for the groups. I found a solution that could help me, the function strtok_r .
http://www.codecogs.com/reference/computing/c/string.h/strtok.php?alias=strtok_r
i think this will isolate full strings so i can read one at time.
Is it a good solution or should take other approach?
Upvotes: 0
Views: 1199
Reputation: 3091
You could also use
https://github.com/leblancmeneses/NPEG/tree/master/Languages/npeg_c
if your search pattern becomes more difficult.
Here is a visual tool that can export C version: http://www.robusthaven.com/blog/parsing-expression-grammar/npeg-language-workbench
Documentation for rule grammar: http://www.robusthaven.com/blog/parsing-expression-grammar/npeg-dsl-documentation
Rules
(?<UU>): 'UU'\i;
(?<II>): 'II'\i;
(?<NJ>): 'NJ'\i;
(?<KU>): 'KU'; // does not use \i so is case sensitive
Find: UU / II / NJ / KU;
(?<RootExpression>): (Find / .)+;
Input 1:
UU, II, NJ, KU uu, ii, nJ, kU
Input 2:
jsdlfj023#uu, ii, nJ, kU $^%900oi)()*() UU, II, NJ, KU
Upvotes: 0
Reputation: 409482
A naive solution would probably to read one character at a time, and when it's 'U'
, 'I'
, 'N'
or 'K'
then read another character to see if it's the next character in the group. If it is then increase a counter for that group. All other characters are simply discarded.
Edit: Example function:
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);
}
Upvotes: 4