Hello Shane,
Let's walk through the possible C functions and how to find the right
one. The standard C functions can be found in any C textbook or at
http://www.rt.com/man/
is an index of all Unix programs and functions - the standard C
functions have (3) as part of the entry in the list.
There is also a pretty good reference of standard C I/O functions at:
http://rabbit.eng.miami.edu/info/functions/stdio.html
Look down until you find the explanation for scanf. There is also a
pretty readable definition of the format string at
http://www.thinkage.ca/english/gcos/expl/c/lib/scanf.html
Note that the description indicates that white space is skipped /
strings stop at white space. That is a good part of the solution and
the first program I provide reflects that. It uses
scanf("%s", ...)
to read each word and then uses a complicated if statement to
determine of the word is matched with or without punctuation.
Digging a little deeper - and IF your C runtime supports this - I
found a solution that appears to handle the more general problem
including the contrived example I provided before. This depends upon
use of something like...
scanf("%[^ .,:;]", ...)
to find words separated by spaces and punctuation and then matches
that. The added getchar is used to skip over the character that we
don't want to use.
To find the functions, I searched using phrases such as:
man page index
c functions stdio
c function scanf
c scanf format
The programs that use these functions are listed at the end of the answer.
Good luck with your work - let me know if you have ANY problems
building or running either program.
--Maniac
// first program - handles normal text
#include <stdio.h>
#include <string.h>
int main() {
int i;
char x[200];
char y[] = "match";
char y1[] = "match.";
char y2[] = "match,";
while (EOF != scanf("%s",&x)) {
// printf(x);
if (strcmp(x,y)==0 ||
strcmp(x,y1)==0 ||
strcmp(x,y2)==0)
printf("match seen.\n");
}
return 0;
}
// second program - also handles the contrived example but might not
work on all systems
#include <stdio.h>
#include <string.h>
int main() {
int i;
char x[200];
char y[] = "match";
while (EOF != scanf("%[^ .,:;]",&x)) {
//printf(x);
if (strcmp(x,y)==0)
printf("match seen.\n");
i = getchar();
}
return 0;
} |
Request for Answer Clarification by
shaneb2000-ga
on
14 May 2004 08:40 PDT
Here is my adaptation:
while (EOF != fscanf(fp1,"%[^ .,:;]",&szWord))
{
nOffset++;
printf("szWord = %s at Offset: %d\n", szWord, nOffset);
getch();
if (strcmp(szWord,argv[1])==0)
printf("match seen.\n");
}
The problem I found with your solution, it would only match the word
if it is the first word in the file and not search the entire file.
Sample Output:
szWord = THE at Offset: 1
szWord = THE at Offset: 2
szWord = THE at Offset: 3
szWord = THE at Offset: 4
szWord = THE at Offset: 5
szWord = THE at Offset: 6
Shane
|
Clarification of Answer by
maniac-ga
on
14 May 2004 15:24 PDT
Hello Shane,
Odd. I don't get the behavior you describe. For example:
./b
Here is a match.
match seen.
Here is notamatch.
match a line for me.
Done.
This does point out a minor problem with the solution I provided - it
needs to have \n\t added to the pattern so it will match after a
newline or tab. When I added that, the result was...
./b
Here is a match.
match seen.
match seen.
Here is a match
match seen.
Here is a match.
match seen.
match seen.
match a line for me.
match seen.
Oops - the getchar() is not enough. We need to consume all the
punctuation / white space characters since x still points to a
matching string. Replacing
getchar();
with
scanf("%[ \n\t.,:;]",&x);
gets the following behavior.
./b
Here is a match.
match seen.
Here is notamatch.
match a line for me.
match seen.
Done.
I suggest you use the version that follows - it still has a two statement loop.
--Maniac
#include <stdio.h>
#include <string.h>
int main() {
char x[200];
char y[] = "match";
while (EOF != scanf("%[^ \n\t.,:;]",&x)) {
//printf(x);
if (strcmp(x,y)==0)
printf("match seen.\n");
scanf("%[ \n\t.,:;]",&x);
}
return 0;
}
|