By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

Character conversion, Can = or != be converted from string?

P: n/a
Hi again to everyone!

First of all i would like to thank all the people in this forum for
their help, i've gone far in my project and i have to thank you for
that! I hope i can repay all the help given.

My question is this, can i make a converstion from a string to an = or
!= Operators?

I have a client program that sends information to a suposed "sql
server" that processes the requests. Both Create and Insert are easy
but Update and Select use = or != operands. Of course i can do a
comparison of the caracters inserted with if and elses but the
eficiency of the program would go sky high if a direct comparison could
be made. For example:

UPDATE Table_name SET colum_name = new_value WHERE colum_name =
old_value

or

SELECT column FROM table WHERE column operator value

My question applies better to the Select command has with the Update
command this question is also easy to solve.
Can this be done? Thanks in advance for any help.

Best Regards

Pedro Pinto

Dec 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Pedro Pinto wrote:
Hi again to everyone!

First of all i would like to thank all the people in this forum for
their help, i've gone far in my project and i have to thank you for
that! I hope i can repay all the help given.

My question is this, can i make a converstion from a string to an = or
!= Operators?
<snip>

Your question is not very clear, but if you're asking whether you can
directly compare two strings with ==, (not =), or !=, then no, C
doesn't have such a feature. You can however use a library function to
compare two or more strings. The standard library functions, strcmp()
and strncmp() do exactly this. Note that they're case sensitive. If you
want to ignore case, convert the strings to a common case via tolower()
or toupper() and then compare.

Dec 5 '06 #2

P: n/a
santosh wrote:
Pedro Pinto wrote:
<snip>
My question is this, can i make a converstion from a string to an = or
!= Operators?
<snip>

Your question is not very clear, but if you're asking whether you can
directly compare two strings with ==, (not =), or !=, then no, [ ... ]
There're also memcmp() and strcoll().

Dec 5 '06 #3

P: n/a
"Pedro Pinto" <ku*****@gmail.comwrites:
My question is this, can i make a converstion from a string to an = or
!= Operators?

I have a client program that sends information to a suposed "sql
server" that processes the requests. Both Create and Insert are easy
but Update and Select use = or != operands. Of course i can do a
comparison of the caracters inserted with if and elses but the
eficiency of the program would go sky high if a direct comparison could
be made. For example:
It's really not clear to me what you're trying to do. It's not
even clear whether you're trying to write the client or the
server program. Can you give some more details?
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Dec 5 '06 #4

P: n/a
Pedro Pinto wrote:
I have a client program that sends information to a suposed "sql
server" that processes the requests. Both Create and Insert are easy
but Update and Select use = or != operands. Of course i can do a
comparison of the caracters inserted with if and elses but the
eficiency of the program would go sky high if a direct comparison could
be made. For example:
There's no way to automatically execute code found in a string. C code
is compiled by a compiler into machine code. Once your program is
running, the compiler is no longer available. If you need to evaluate
arbitrary expressions supplied at run time, you must write your own
interpreter.
UPDATE Table_name SET colum_name = new_value WHERE colum_name =
old_value

or

SELECT column FROM table WHERE column operator value
You want to write a SQL server? You want to write a program that
understands and executes UPDATE and SELECT commands?

You need to parse the command into an array or list of tokens:

{"SELECT", "column", "FROM", "table",
"WHERE", "column", "operator", "value"}

Then identify the keywords, and get the variable data (column name, the
table name, etc). You will need to write code that searches through the
data. The simplest way would be to go through each record in your
database and match it against the given conditions.

Here's some pseudocode. You need to implement the functions parse,
get_value, and add_result.

parse("SELECT name FROM classmates WHERE sex = female",
select_column,
select_table,
where_column,
where_operator,
where_value);

/* After tokenisation and parsing now
select_column is "name",
select_table is "classmates",
where_column is "sex",
where_operator is "=",
and where_value is "female"
*/

for(row = 0; row < num_rows; row++)
{
const char *data_value = get_value(row, where_column);
if(!strcmp(where_operator, "=")
{
if(!strcmp(where_value, data_value)) add_result(row);
}
else if(!strcmp(where_operator, "!=")
{
if(strcmp(where_value, data_value)) add_result(row);
}
else
{
fprintf(stderr, "Unrecognised operator %s\n", where_operator);
}
}

--
Simon.
Dec 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.