OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Apr 27, 2024 10:22 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Strings in C
PostPosted: Thu Jan 30, 2003 3:30 pm 
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[u] != cmp_str[u]) return 0;
u++;
}
return 1;
}

Please help me quick as I will fall asleep soon....


Top
  
 
 Post subject: Re:Strings in C
PostPosted: Thu Jan 30, 2003 3:48 pm 
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:
char strcmp(const char *s1, const char *s2)
{
   while ((*s2 != '\0') && (*s1==*s2))
   {
      s1++;
      s2++;
   }
   return (*s1 - *s2);
}


Top
  
 
 Post subject: Re:Strings in C
PostPosted: Thu Jan 30, 2003 3:53 pm 
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 :D)


Top
  
 
 Post subject: Re:Strings in C
PostPosted: Fri Jan 31, 2003 2:01 am 
--- 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


Top
  
 
 Post subject: Re:Strings in C
PostPosted: Fri Jan 31, 2003 3:20 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
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)

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:Strings in C
PostPosted: Fri Jan 31, 2003 3:42 am 
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


Top
  
 
 Post subject: Re:Strings in C
PostPosted: Fri Jan 31, 2003 3:48 am 
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


Top
  
 
 Post subject: Re:Strings in C
PostPosted: Fri Jan 31, 2003 4:22 am 
Quote:
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:
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:
int strcmp(const char *s, const char *t)
{
char *sPointer = s, *tPointer = t;
for (...)
...
}

best regards,
A. Blessing


Top
  
 
 Post subject: Re:Strings in C
PostPosted: Fri Jan 31, 2003 5:16 am 
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


Top
  
 
 Post subject: Re:Strings in C
PostPosted: Fri Jan 31, 2003 5:32 am 
You're right, I messed up some things...thanks


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Amazonbot [bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group