Google Answers Logo
View Question
 
Q: Search and find a word seperated by whitespace or punctuation in C ( Answered,   0 Comments )
Question  
Subject: Search and find a word seperated by whitespace or punctuation in C
Category: Computers > Algorithms
Asked by: shaneb2000-ga
List Price: $5.00
Posted: 12 May 2004 11:19 PDT
Expires: 11 Jun 2004 11:19 PDT
Question ID: 345237
I need write write a simple code in C, that will search though a file
seperated by whitespace or punctuation. The tricky part I need to
write the code that looks for a match (including parses the input) in
only 2 statement using only the standard C library functions.

"The code that loops through the file and looks for a match to the
sought-after word can be written as a loop containing just two
statements in its body. A statement can contain functions but they can
only be Standard C Library function. (A statement is a line of code
that has a single semicolon that occurs at the end of the line.) "

Thanks      
Shane

Request for Question Clarification by maniac-ga on 12 May 2004 17:53 PDT
Hello Shane,

An interesting question. I have a "two statement" solution but it is
limited to matching something like:
 - match.
 - match,
 - match:
(and can be generalized for any punctuation at the start or end of a word)
which handles normal text but does not handle contrived examples such as:
 - a.match.b
where the word is between two punctuation marks.

Is that an acceptable solution?
  --Maniac

Clarification of Question by shaneb2000-ga on 13 May 2004 12:25 PDT
Ya, it sounds what it is suppost to do.
Thanks
Answer  
Subject: Re: Search and find a word seperated by whitespace or punctuation in C
Answered By: maniac-ga on 13 May 2004 15:57 PDT
 
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;
}
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy