Does anybody know where to find the code for the function(s) strcmp() or strcasecmp(). I need the actual code, not the header file for them. Thanks for any help...
Does anybody know where to find the code for the function(s) strcmp() or strcasecmp(). I need the actual code, not the header file for them. Thanks for any help...
Greetings,
There are a couple of ways this function can be written. Some use for loops, while loops, do-while loops, and so forth, but it all comes down to the end of comparing string1 to string2.
All we really have to do to write such a function is:
- Loop through string1 and string2
- Check if current character of string1 is identical to string2
- Check existance of current character for string1 and string2 [make sure they are valid]
- Increment both places if they match
- Return difference of string1 and string2
If this makes sense, then writing a simple verison shouldn't be difficult. If you have further questions, please feel free to ask.
- Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
The other one does not exist on my machine./***
*strcmp.c - routine to compare two strings (for equal, less, or greater)
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* Compares two string, determining their lexical order.
*
************************************************** *****************************/
#include <cruntime.h>
#include <string.h>
#ifdef _MSC_VER
#pragma function(strcmp)
#endif /* _MSC_VER */
/***
*strcmp - compare two strings, returning less than, equal to, or greater than
*
*Purpose:
* STRCMP compares two strings and returns an integer
* to indicate whether the first is less than the second, the two are
* equal, or whether the first is greater than the second.
*
* Comparison is done byte by byte on an UNSIGNED basis, which is to
* say that Null (0) is less than any other character (1-255).
*
*Entry:
* const char * src - string for left-hand side of comparison
* const char * dst - string for right-hand side of comparison
*
*Exit:
* returns -1 if src < dst
* returns 0 if src == dst
* returns +1 if src > dst
*
*Exceptions:
*
************************************************** *****************************/
int __cdecl strcmp (
const char * src,
const char * dst
)
{
int ret = 0 ;
while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
Way to go posting the homework answers
I hardly think "Find where strcmp() is located and then print off the source code would be a homework question"...
And if the homework question is, "Write a function that compares two strings", how smart would it be to hand-in strcmp() and expect nobody to notice?
I'm a bit vague as to where the problem is with displaying code that is readily available on anybody's machine that has the C Language installed. Granted, I could have made it a bit more challenging by suggesting a search for "String.c*" on their computer, or to look up the Definition through MSVC++ 6.0, but still, 5 frowny faces seem a bit excessive to me.
Sometimes our efforts seem feckless. The fast path to answers will only suffice for a short while. Or so I hope.Originally Posted by Salem
I beg to differ. Check these out:Originally Posted by Epo
Even though the homework deadlines may be 10 years old, roughly, this tells us that there are homework questions like this.
- Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Compared to your two examples of homework assignments where it seems the task is to re-create a similar strcmp() function, how many people out there do you think genuinely would like to know how strcmp() works, so that they actually know what's going on when they use it?
It's great to know that strcmp() returns 0 if the strings are equal, scanf() can return 0 if the scan fails, or the number of validly entered arguments if it does not fail, but isn't another good question: "Why?".
> how many people out there do you think genuinely would like to know how strcmp() works, so that they actually know what's going on when they use it?
» Alot of people. They may not need the experience now, but why wait when you need it?
Let's make a parallel. "How do computers work?" I'm pretty sure some people know how they work and can build/re-build a machine if they wanted too. If someone else built a machine before-hand does not mean you can too, unless you know how. Though in order to do this, you have to know how it works. One thing misplaced, and the machine may not run optimally, or at all.
All in all, re-writing a function that already exists can help the programmer understand how it works. Asking a programmer to write a function that compares two strings has the same concept as if they were asked to compare two files. The logic of code is better understood learned rather than given.
- Stack Overflow
Last edited by Stack Overflow; 11-16-2004 at 04:54 PM. Reason: More info.
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
strcasecmp is the name of the PHP function to compare strings disregarding case. In C it's stricmp (note the 'i') and it's declared in string.h. stricmp is part of ANSI C standard, so don't bother using it.Originally Posted by egomaster69
The stricmp() case-insensitive string comparison function is not part of the C standard, but it is a common extension on many C compilers.Originally Posted by xErath
Furthermore, stricmp is NOT part of the C or C++ standard. Many compilers support this function, but it's an addon, or compiler specific function, and not part of either standards.
- Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Upps.. I though it was.. Been goggling a bit. I did found however some std info on strings and stricmp wasn't there. Thanks Stack Overflow for correcting me
But still don't bother using stricmp. It's a very popular function.
Any lecturer would know that this code is not of a student:
And a great deal of people need to see something before they truely undertand it. People learn in different ways, but as an answer to homework this code would not be any benefit by a simple copy and paste, re-writing the function (in a more simplistic way i imagine) would be the outcome and an understanding would be achieved.Code:int __cdecl strcmp ( const char * src, const char * dst ) { int ret = 0 ; while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst) ++src, ++dst; if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); }
Microsoft is merely an illusion, albeit a very persistant one.
Originally Posted by Salem
way to go posting intellectual property of someone else you mean...
Well now that depends. I've never taken a C class, but I venture, were I to do so, I could turn in some code the teacher wouldn't expect. For example, here's the answer to one of the more common homework problems:
At any rate, you're intentionally missing the point. Go read the announcements. Read my SIG while you're at it.Code:void O_Oy( char *o_o, int o_O ) { char V_V[BUFSIZ]="%s\n"; char T_T[BUFSIZ]="%%[^%s]"; char v_v[BUFSIZ]={0}; char x_x[BUFSIZ]={0}; while( o_O -- ) { sprintf( v_v, T_T, o_o + o_O + 1 ); sscanf( o_o, v_v, x_x ); printf( V_V, x_x ); } } int main( void ) { char o_o[] = "1234567890"; O_Oy( o_o, 0x9 ); return 0; }
Quzah.
Hope is the first step on the road to disappointment.
Originally Posted by jwentingThe copyright is there.../***
*strcmp.c - routine to compare two strings (for equal, less, or greater)
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* Compares two string, determining their lexical order.
*
************************************************** *****************************/