Page 1 of 1
Strings in C
Posted: Thu Jan 30, 2003 3:30 pm
by chrisa128
Sorry about this question being so simple, I am just too tired to think straight.
If I wanted to compare text entered by the user would I have to check it bit by bit like the following
unsigned strcmp(unsigned usr_str,unsigned cmp_str)
{
unsigned c, u;
c = strlen(usr_str);
while (u =< c)
{
If (usr_str != cmp_str) return 0;
u++;
}
return 1;
}
Please help me quick as I will fall asleep soon....
Re:Strings in C
Posted: Thu Jan 30, 2003 3:48 pm
by drizzt
Certainly this is not the best strcmp procedure i have seen...
First of all both strings passed to the procedure should be pointers and not unsigned vars... and you can check the end of a string in a faster way, simply checking the character '\0'.
Here is my strcmp routine:
Code: Select all
char strcmp(const char *s1, const char *s2)
{
while ((*s2 != '\0') && (*s1==*s2))
{
s1++;
s2++;
}
return (*s1 - *s2);
}
Re:Strings in C
Posted: Thu Jan 30, 2003 3:53 pm
by Slasher
You have the right idea, but this is better
int strcmp(char *str1,char *str2)
{
/*loops till one of the chars do not match*/
while(*str1++ == *str2++ );
if(*str1==*str2)
return 0;
else
if(*str1 < *str2)
return -1;
else
return 1;
}
try this, should work!
(ps Drizzt replied before i finished
)
Re:Strings in C
Posted: Fri Jan 31, 2003 2:01 am
by klalafuda klalafu
--- cut ---
1. while(*str1++ == *str2++ );
2. if(*str1==*str2)
...
--- cut ---
now immagine, that str1 == str2 == { 'a', b', 'c' '\0'};
so, at the line 2 you will reference an unallocated memory ahead of EOS char ('\0'). likely, nothing's too serious should happen, but possibly it's SIGSEGV
// wbr
Re:Strings in C
Posted: Fri Jan 31, 2003 3:20 am
by Pype.Clicker
the 'segfault will probably be unfrequent, but i agree it could occur.
Moreover, with slasher's code, if you have
str1 = "TST\0a"; str2 = TST\0b";
the two strings will appear distinct (the they'll end with 'a'<'b') while they are equal at 'strcmp' semantic (strings end at first 0 character)
Re:Strings in C
Posted: Fri Jan 31, 2003 3:42 am
by chrisa128
Thanks all, now I got another problem because I am using a structure for allmy Virtual Consoles. When I try to pass the field in the structure like the following
strcmp(_vcon[_curr_vc].cmd_buf[], strcmd1);
I get the passing interger as pointer warning which messes it up completely. I am going to try and replace it with
strcmp(_vcon[_curr_vc].cmd_buf, strcmd1);
as soon as I get to the computer which I use for development but it would saveme time if you could advise, me on this.
Thanks once again for your help so far, esspecially about the segfault, I will have a look at that later.
Chris
Re:Strings in C
Posted: Fri Jan 31, 2003 3:48 am
by klalafuda klalafu
well, if finally walking the way above, WATCOM C RTL gives the following:
--- cut ---
strcmp(const char *s, const char *t ) {
for( ; *s == *t; s++, t++ )
if( *s == '\0' )
return 0;
return (*s - *t);
}
--- cut ---
// wbr
Re:Strings in C
Posted: Fri Jan 31, 2003 4:22 am
by Whatever5k
strcmp(const char *s, const char *t ) {
for( ; *s == *t; s++, t++ )
if( *s == '\0' )
return 0;
return (*s - *t);
}
I'm not really sure if this strcmp() function is really good...
It's the right idea, but I wouldn't increment the "original" strings since this may cause problems...
for example:
Code: Select all
int isEqual;
char bla[] = "Hello";
char bla2[] = "Hello2";
printf("*bla = %c\n", *bla);
isEqual = strcmp(bla, bla2);
printf("*bla = %c\n", *bla);
The first printf() call would print a 'H' as it should. The second call would print a 'o' since you modified the pointer by incrementing it...
better is:
Code: Select all
int strcmp(const char *s, const char *t)
{
char *sPointer = s, *tPointer = t;
for (...)
...
}
best regards,
A. Blessing
Re:Strings in C
Posted: Fri Jan 31, 2003 5:16 am
by klalafuda klalafu
sorry, but you'r wrong
the case above would take place if the function was defined like:
strcmp(const char **s, const char **t );
// wbr
Re:Strings in C
Posted: Fri Jan 31, 2003 5:32 am
by Whatever5k
You're right, I messed up some things...thanks