Strings in C
Strings in C
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....
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
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:
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
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 )
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
--- 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
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
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Strings in C
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)
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
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
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
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
--- 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
I'm not really sure if this strcmp() function is really good...strcmp(const char *s, const char *t ) {
for( ; *s == *t; s++, t++ )
if( *s == '\0' )
return 0;
return (*s - *t);
}
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);
better is:
Code: Select all
int strcmp(const char *s, const char *t)
{
char *sPointer = s, *tPointer = t;
for (...)
...
}
A. Blessing
Re:Strings in C
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
strcmp(const char **s, const char **t );
// wbr