473,378 Members | 1,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Boolean search in C

Hello people, I'm a new member of this fantastic community!
I'm a student and i have to solve this puzzle:

I have a string like
"The quick brown fox jumps over the lazy dog" (or longer)

I have to search through this string using boolean operators like
"quick AND brown OR fox"

mantaining the logical order, so "quick AND (brown OR fox)".

(consider that the search string can be longer with n search options).

What's the strategy?

I can recursively tokenize the string by splitting via strtok()? but how to mantain operator precedence?

If anyone can help me or if you know a good solution pointing me to the right way i'll appreciate!
Thanks!
Valerio
Feb 5 '08 #1
3 3766
gpraghuram
1,275 Expert 1GB
Hello people, I'm a new member of this fantastic community!
I'm a student and i have to solve this puzzle:

I have a string like
"The quick brown fox jumps over the lazy dog" (or longer)

I have to search through this string using boolean operators like
"quick AND brown OR fox"

mantaining the logical order, so "quick AND (brown OR fox)".

(consider that the search string can be longer with n search options).

What's the strategy?

I can recursively tokenize the string by splitting via strtok()? but how to mantain operator precedence?

If anyone can help me or if you know a good solution pointing me to the right way i'll appreciate!
Thanks!
Valerio
My idea is, you can tokenize the string and look for words which represents operators like and or etc.
As you encounter the word push it to a separate stack and continue tokenizing.
Finally at the end of iteration you will have all the operators in the stack.
Please add more info and i will try to help u

Raghuram
Feb 6 '08 #2
My idea is, you can tokenize the string and look for words which represents operators like and or etc.
As you encounter the word push it to a separate stack and continue tokenizing.
Finally at the end of iteration you will have all the operators in the stack.
Please add more info and i will try to help u

Raghuram
Raghuram
thanks for the answer. I created my own strategy to solve this:
1) tokenize the search string by spaces
2) eval every token
2.1)if token is AND, OR transform to &, |
2.2)if token is a word, use strstr to find if the token is on the searched string. if token is found, return 1, otherwise 0.
3) at the end of the loop, i have a string like "1&0|1" (one and zero or one).

If you evaluate this, you can know if the search string can return values.

Following the code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char *converti(char *token, char *campione);
  5.  
  6. int main(int argc, char *argv[]) {
  7.     char titolo[] = "la vispa teresa correa tra l'erbetta e con un calcio moria la minchietta";
  8.     char ricerca[] = "vispa and jeeg";
  9.  
  10.     char *pch;
  11.     char result[] = "\0";
  12.     pch = strtok(ricerca, " ");
  13.     while(pch != NULL) {
  14.         strcat(result, converti(pch, titolo));
  15.         pch = strtok(NULL, " ");
  16.     }
  17.     printf("%s\n", result);
  18.     return 0;
  19. }
  20.  
  21. char *converti(char *token, char *campione) {
  22.     if( strcmp(token, "and") == 0 ) 
  23.         return "&";
  24.     else if( strcmp(token, "or") == 0 )
  25.         return "|";
  26.     else
  27.         if( strstr(campione, token) != NULL )
  28.             return "1";
  29.         else
  30.             return "0";
  31. }
  32.  
Now the computer can evaluate the boolean problem... but, i think that the atoi() function can evaluate and transform, but it evaluate the first integer, not the whole expression. D'OH!

Can you solve this?
Feb 6 '08 #3
gpraghuram
1,275 Expert 1GB
Raghuram
thanks for the answer. I created my own strategy to solve this:
1) tokenize the search string by spaces
2) eval every token
2.1)if token is AND, OR transform to &, |
2.2)if token is a word, use strstr to find if the token is on the searched string. if token is found, return 1, otherwise 0.
3) at the end of the loop, i have a string like "1&0|1" (one and zero or one).

If you evaluate this, you can know if the search string can return values.

Following the code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char *converti(char *token, char *campione);
  5.  
  6. int main(int argc, char *argv[]) {
  7.     char titolo[] = "la vispa teresa correa tra l'erbetta e con un calcio moria la minchietta";
  8.     char ricerca[] = "vispa and jeeg";
  9.  
  10.     char *pch;
  11.     char result[] = "\0";
  12.     pch = strtok(ricerca, " ");
  13.     while(pch != NULL) {
  14.         strcat(result, converti(pch, titolo));
  15.         pch = strtok(NULL, " ");
  16.     }
  17.     printf("%s\n", result);
  18.     return 0;
  19. }
  20.  
  21. char *converti(char *token, char *campione) {
  22.     if( strcmp(token, "and") == 0 ) 
  23.         return "&";
  24.     else if( strcmp(token, "or") == 0 )
  25.         return "|";
  26.     else
  27.         if( strstr(campione, token) != NULL )
  28.             return "1";
  29.         else
  30.             return "0";
  31. }
  32.  
Now the computer can evaluate the boolean problem... but, i think that the atoi() function can evaluate and transform, but it evaluate the first integer, not the whole expression. D'OH!

Can you solve this?

There is a issue in the code
You are doing a array overwrite here
Expand|Select|Wrap|Line Numbers
  1. char result[] = "\0"; //The size of the array is 1
  2.     pch = strtok(ricerca, " ");
  3.     while(pch != NULL) {
  4.         strcat(result, converti(pch, titolo)); //This causes array
  5.                                                                                 //overwrite
  6.         pch = strtok(NULL, " ");
  7.     }
  8.  
  9.  
Try to use dynamic allocation for the variable result
atoi converts the ascii string to a number is the ascii string contains a number

Raghuram
Feb 14 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: David | last post by:
Hi, I'm trying to add a search facility to a page that looks for matches in one, other or both memo fields of a database. The code below works fine if the visitor types in one word, or the term...
1
by: leegold | last post by:
Show full header Is there any way to make relevance when using boolean mode more useful? If not, are there plans in the Fulltext development "todo" for making it useful? I'm thinking of just...
0
by: phillip.s.powell | last post by:
SELECT id, student_first_name, student_last_name, major, minor , MATCH(major, minor) AGAINST ('"mechanical engineering"') AS score FROM students WHERE MATCH(major, minor) AGAINST ('"mechanical...
1
by: keithb | last post by:
The following code sets a DataTable column DataType to "String" column.DataType = System.Type.GetType("System.String"); What is the corresponding statement when the data type is boolean? I...
0
by: Malte | last post by:
Hello there, We got a problem atm with our 20k database. Everytime we search for sth. with double quotes it takes much more longer than without. Here is what we did: mysql> SELECT COUNT(*)...
3
by: Corey-g via AccessMonster.com | last post by:
Hi All, I did search for this before posting but I didn't find what I was looking for. .. I have a function (returning boolean) that in turn calls 2 more functions (also returning boolean). ...
4
by: Phaelle | last post by:
Hi! I am doing a search engine and I would like to make a search with the operator AND possible but I canīt find out how. Do I have to explode the phrase entered after each and" or does it exist an...
0
by: Rob Meade | last post by:
Hi all, Before I embark on a quest to improve our site search I was wondering if anyone knew of any good articles or has any advice on how to achieve what I want. I want to be able to allow...
4
by: Ironr4ge | last post by:
Hi everyone, I am trying to open the form "Languages" with a diffrent record source to the "Contacts" form where I conducted the search or filter... . I was wondering whether there was a vba...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.