Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to comape tokenised strings?? 1

Status
Not open for further replies.

lbucko

Programmer
Feb 13, 2002
11
0
0
IE
I have a file which contains certain text strings. I also have a char array which contains a query string. I have tokenised both strings to take one word at at a time but am unsure as how to compare the tokens to find out if there is a match between them. What I am trying to achieve is to search a file for a given text query, Code is as follows:
Code:
void smart_2(char *query, Res results[][100])
	{
		
		char string[] = "Distributed IR";
		char *tokenPtr;		//Token Ptr to query file
		char *ttokenPtr;	//Token Ptr to initial query string
		
		fp = fopen("query_file.txt", "r");	//Query file
		ffp = fopen("temp.txt", "w");		//Temp file to write tokens to

		while(!feof(fp))
		{
			fprintf(ffp, "%s\n", " ", string);
			
			tokenPtr = strtok(string, " ");

			while(tokenPtr != NULL)
			{
				fscanf(fp,"%s", strtok);
				fprintf(ffp, "%s\n", tokenPtr);
				
				tokenPtr = strtok(NULL, " ");
			}//while tokPtr

		}//while eof

		printf("%s\n", " ", query);
		ttokenPtr = strtok(query, " ");

		while(ttokenPtr !=NULL)
		{
			printf("%s\n", ttokenPtr);
			ttokenPtr = strtok(NULL, " ");
		}//while ttokPtr

	}//smart_2
Any ideas on how to do this or improvements would be appreciated.
 
If you are only searching for a given text query,perhaps you dont need to tokenised the strings,you only need to use "strstr" to find the specific text query that you are searching for.

try this:

bool SearchQuery( FILE *file, char *string )
{
bool found = false;
char phrase[100]; // you have to choose any convenient size for this array
int i =0;
while( string != '\0')
{
tolower( string );
i++;
}
while(( fgets( phrase, 100, file ) != NULL ) && ( found == false ))
{
if( strstr( phrase, string ) != NULL )
{
found = true;
}
}
if( found == true )
return 1;
else
return 0;
}
 
Thanks Leibnitz, that worked alright but I'm still a bit puzzeled as how to manage queries with more than one word. can I index the individual words in the string in some way??
I was trying to add another while loop inside the following loop,
Code:
while( string[i] != '\0')
    {
       tolower( string[i] );
       i++;
    }

which would check when the query got a whitespace.
Code:
while (query[i]!= " ")

Not sure if that makes any sense, would I need to check an array of words??
 
well,it does'nt mather if the "query" got more than one word or whitespaces between each word,the previous code should work for those cases too.


good luck !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top